app.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //app.js
  2. import { systemInfo, routers, isFn, getGlobalVal, getWxUserInfoSetting, getUpdateManager } from './utils/util.js'
  3. import { WxLogin, FirstWxLogin, GetMineInfo } from './utils/api.js'
  4. const { reLaunch, redirectTo } = routers()
  5. App({
  6. onLaunch: function (options) {
  7. this.globalData.options = options
  8. getUpdateManager()
  9. wx.getSystemInfo({
  10. success: e => {
  11. let modelmes = e.model;
  12. if (modelmes.search('iPhone X' || 'iPhone 11') != -1) {
  13. this.globalData.isIphoneX = true
  14. }
  15. }
  16. })
  17. wx.onMemoryWarning(function (e) {
  18. console.log('onMemoryWarningReceive', e)
  19. })
  20. },
  21. onShow: function (options) {
  22. this.globalData.options = options
  23. },
  24. onHide: function (e) {
  25. },
  26. onError: function (e) {
  27. wx.showToast({
  28. title: '程序执行失败',
  29. icon: 'none'
  30. })
  31. },
  32. onPageNotFound: function (e) {
  33. wx.showToast({
  34. title: '没有找到对应页面',
  35. icon: 'none'
  36. })
  37. setTimeout(() => {
  38. redirectTo({ url: 'pages/index/index' })
  39. }, 2000)
  40. },
  41. /**
  42. * 初始化
  43. */
  44. init: function () {
  45. // redirectTo({ url: 'president/pages/home/home' })
  46. // reLaunch({ url: 'dispatcher/pages/withdraw/withdraw'})
  47. // reLaunch({ url: 'dispatcher/pages/dispatch_go/dispatch_go'})
  48. // reLaunch({ url: 'parents/pages/home/home'})
  49. // reLaunch({ url: 'dispatcher/pages/home/home' })
  50. // return
  51. const userInfo = getGlobalVal('userInfo')
  52. getWxUserInfoSetting((type, res) => {
  53. // if (type != 1 || !userInfo) {
  54. // redirectTo({ url: 'pages/wx_authority/wx_authority' })
  55. // return
  56. // }
  57. this.globalData.userInfo = userInfo
  58. wx.checkSession({
  59. success: res => {
  60. const arr1 = ['accessToken', 'userInfo', 'userId']
  61. const arr2 = ['vipList', 'vip', 'vipIndex', 'vipId']
  62. const temp = {}
  63. for (let i in arr1) {
  64. temp[arr1[i]] = getGlobalVal(arr1[i])
  65. }
  66. for (let i in arr2) {
  67. temp[arr2[i]] = getGlobalVal(arr2[i], `${arr2[i]}_${temp['userId']}`)
  68. }
  69. for (let i in temp) {
  70. this.globalData[i] = temp[i]
  71. }
  72. this.permissionsJumpFn()
  73. },
  74. fail: res => {
  75. this.login()
  76. }
  77. })
  78. })
  79. },
  80. /**
  81. * 获取微信code
  82. */
  83. getWxloginCode: function (cb) {
  84. wx.login({
  85. success: res => {
  86. if (isFn(cb)) cb(res.code)
  87. },
  88. fail: res => {
  89. wx.showToast({
  90. title: '获取微信code失败',
  91. icon: 'none'
  92. })
  93. }
  94. })
  95. },
  96. /**
  97. * 获取微信用户信息
  98. */
  99. login: function () {
  100. this.getWxloginCode(code => {
  101. WxLogin({ data: { code } }).then(this.handleLogin).catch(e => {
  102. if (e.data.code == '888') {
  103. redirectTo({ url: 'pages/wx_authority/wx_authority' })
  104. }
  105. })
  106. })
  107. },
  108. /**
  109. * 处理登录信息
  110. */
  111. handleLogin: function (res) {
  112. const { accessToken, userId, isDispatcher, list } = res.data
  113. const arr = { accessToken, userId, isDispatcher, list }
  114. for (let i in arr) {
  115. this.globalData[i] = arr[i]
  116. wx.setStorageSync(i, arr[i])
  117. }
  118. this.permissionsJumpFn()
  119. },
  120. /**
  121. * 权限跳转
  122. */
  123. permissionsJumpFn: function (e) {
  124. const { tempOptions, options } = this.globalData
  125. const isDispatcher = getGlobalVal('isDispatcher')
  126. const userId = getGlobalVal('userId')
  127. const libId = getGlobalVal(`dispatchLibId_${userId}`)
  128. const userInfo = getGlobalVal('userInfo')
  129. const { query = {}, scene, path } = options || tempOptions
  130. let url = ''
  131. if (query && query.type > 1) {
  132. url = path ? path : 'pages/index/index'
  133. } else {
  134. // url = 'dispatcher/pages/sweeped_books/sweeped_books'
  135. if (isDispatcher == 1) {
  136. if (!userInfo) {
  137. redirectTo({ url: 'pages/wx_authority/wx_authority' })
  138. return
  139. }
  140. if (libId) {
  141. // url = 'dispatcher/pages/dispatch_leave/dispatch_leave'
  142. url = 'dispatcher/pages/dispatch_go/dispatch_go'
  143. } else {
  144. url = 'dispatcher/pages/home/home'
  145. }
  146. } else if (isDispatcher == 3) {
  147. // console.log('111')
  148. if (!userInfo) {
  149. redirectTo({ url: 'pages/wx_authority/wx_authority' })
  150. return
  151. }
  152. url = 'president/pages/home/home'
  153. } else {
  154. url = 'parents/pages/main/main'
  155. this.getVipList()
  156. }
  157. }
  158. const paras = { ...query, url, scene }
  159. // console.log(this.globalData)
  160. // console.log(paras)
  161. if (isFn(reLaunch)) reLaunch({ url, paras })
  162. this.globalData.options = tempOptions
  163. },
  164. /**
  165. * 获取会员卡列表
  166. */
  167. getVipList: function () {
  168. const continuousFn = { fn: this.getVipList, param: { ...arguments } }
  169. const userId = getGlobalVal('userId')
  170. const vipId = getGlobalVal('vipId', `vipId_${userId}`)
  171. const { noVipListToastText } = this.globalData
  172. const fn = (i, v) => {
  173. this.globalData['vip'] = v
  174. wx.setStorageSync(`vip_${userId}`, v)
  175. this.globalData['vipIndex'] = i
  176. wx.setStorageSync(`vipIndex_${userId}`, i)
  177. this.globalData['vipId'] = v['vip_id']
  178. wx.setStorageSync(`vipId_${userId}`, v['vip_id'])
  179. }
  180. GetMineInfo({ continuousFn }).then(res => {
  181. const { vipList = null } = res.data
  182. if (vipList.length > 0) {
  183. let isSame = false
  184. for (let i in vipList) {
  185. if (vipList[i]['vip_id'] == vipId) {
  186. isSame = true
  187. fn(i, vipList[i])
  188. break
  189. }
  190. }
  191. if (!isSame) {
  192. fn(0, vipList[0])
  193. }
  194. this.globalData['vipList'] = vipList
  195. wx.setStorageSync(`vipList_${userId}`, vipList)
  196. } else {
  197. wx.showToast({
  198. title: noVipListToastText,
  199. icon: 'none'
  200. })
  201. }
  202. })
  203. },
  204. /**
  205. * 保存微信用户数据
  206. */
  207. saveUserInfo: function (res, code) {
  208. const { accessToken, userId } = this.globalData
  209. const { encryptedData, iv, signature, userInfo } = res
  210. const user = Object.assign(userInfo, { encryptedData, iv, signature })
  211. FirstWxLogin({ data: { code, ...user } }).then(res => {
  212. this.globalData.userInfo = user
  213. wx.setStorageSync('userInfo', user)
  214. wx.setStorageSync('hiddenModel', true)
  215. wx.setStorageSync('isfirstLogin', true)
  216. wx.setStorageSync('hiddenGetallModel', true)
  217. wx.setStorageSync('infofrominput', [])
  218. this.handleLogin(res)
  219. })
  220. },
  221. /**
  222. * 是否有会员卡服务
  223. */
  224. hasVipService: function () {
  225. const vipId = getGlobalVal('vipId')
  226. if (vipId) {
  227. return true
  228. } else {
  229. wx.showToast({
  230. title: this.globalData.noVipServiceToastText,
  231. icon: 'none'
  232. })
  233. return false
  234. }
  235. },
  236. /**
  237. * 是否有当前图书馆服务
  238. */
  239. hasLibraryService: function () {
  240. const { stype = 0, id } = getGlobalVal('library')
  241. if (!id) {
  242. wx.showToast({
  243. title: this.globalData.noLibraryToastText,
  244. icon: 'none'
  245. })
  246. return false
  247. } else {
  248. if (stype == 1) {
  249. return true
  250. } else {
  251. wx.showToast({
  252. title: this.globalData.noLibraryServiceToastText,
  253. icon: 'none'
  254. })
  255. return false
  256. }
  257. }
  258. },
  259. /**
  260. * 全局变量
  261. */
  262. globalData: {
  263. systemInfo,
  264. wechatsys: systemInfo.version + ' ' + systemInfo.SDKVersion,
  265. phoneimei: '',
  266. phonesys: systemInfo.system,
  267. phonetype: systemInfo.brand + ' ' + systemInfo.model,
  268. version: '1.3.3',
  269. platform: '3',
  270. build: '1.3.3',
  271. initStatus: 0,
  272. loginTime: 0,
  273. appid: 'wxe9cdca7c3365e448',
  274. // host: 'https://t.sharingschool.com/gz',
  275. // baseImgUrl: 'https://img.sharingschool.com',
  276. // thumbnail: '?x-oss-process=image/resize,m_fill,h_300,w_200',
  277. host: 'https://xt.sharingschool.com/gz',
  278. baseImgUrl: 'https://xtimg.sharingschool.com',
  279. thumbnail: '!t200x300.jpg',
  280. tempOptions: { query: {}, path: 'pages/index/index', scene: 1001 },
  281. options: null,
  282. continuousFn: null,
  283. accessToken: null,
  284. userId: null,
  285. vipList: null,
  286. vip: null,
  287. vipIndex: null,
  288. vipId: null,
  289. userInfo: null,
  290. isIphoneX:null,
  291. noVipListToastText: '您尚未开通私塾家共享图书馆借阅服务,请就近至私塾家共享图书馆开通服务',
  292. noVipServiceToastText: '您非本馆会员,无法进行此操作,如想继续操作可联系管理员',
  293. noLibraryServiceToastText: '您非本馆会员,无法进行此操作,请切换到您所在的馆继续操作',
  294. noLibraryToastText: '您暂未选择图书馆,请回到首页进行定位匹配',
  295. noBorrowToastText: '您已经预约过此书',
  296. qrcodeInvalidToastText: '二维码已失效',
  297. borrowToastText: '此书库存有货,点击我想借阅将在下一次调书时移至您的服务所在馆',
  298. reservationsToastText: '您可直接扫描书本上二维码借阅,也可预约,如预约,其他人将暂时不可借。',
  299. wishToastText: '更多人将此书加入心愿单会成为我们的优先采购书籍'
  300. }
  301. })