index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // pages/index/index.js
  2. import { homePage } from '../../api/index'
  3. import { createQRcode } from "../../api/document"
  4. import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. locationStr: '定位地址…',
  12. useNumber: 0, // 剩余预约次数
  13. documentVos: [], // 档案信息
  14. cardNo: '',
  15. cardSecret: '',
  16. scanTimer: null,
  17. baseStr: 'data:image/jpg;base64,',
  18. QRCodeBase64: '',
  19. qrcodeDialog: false,
  20. qrUserName: '',
  21. phoneAuthShow: false,
  22. userAuthShow: false,
  23. avatarUrl: '',
  24. nickName: ''
  25. },
  26. onShow () {
  27. app.globalData.selectedInex = 0
  28. },
  29. onLoad () {
  30. this.init()
  31. },
  32. // 初始化函数,判读需不需要获取wx.login => code
  33. init () {
  34. console.log(app.globalData.accessToken, 'index onlaod')
  35. const accessToken = app.globalData.accessToken
  36. if (accessToken) {
  37. var isNeedPhone = wx.getStorageSync('isNeedPhone')
  38. var isNeedHeadImg = wx.getStorageSync('isNeedHeadImg')
  39. var headImg = wx.getStorageSync('headImg')
  40. var userName = wx.getStorageSync('userName')
  41. var phoneNumber = wx.getStorageSync('phoneNumber')
  42. app.globalData.isNeedPhone = isNeedPhone
  43. app.globalData.isNeedHeadImg = isNeedHeadImg
  44. app.globalData.userInfo.headImg= headImg
  45. app.globalData.userInfo.userName = userName
  46. app.globalData.userInfo.phoneNumber = phoneNumber
  47. app.globalData.userInfo.login = true
  48. this.handleAuth()
  49. } else {
  50. this.appCodeLoginFn()
  51. }
  52. },
  53. // 是否需要权限判断 手机号 头像 昵称
  54. handleAuth () {
  55. // 不需要手机号授权 有头像和昵称
  56. const resIsNeedPhone = app.globalData.isNeedPhone
  57. const resHeadImg = app.globalData.userInfo.headImg
  58. const that = this
  59. if (!resIsNeedPhone && resHeadImg !== '') {
  60. app.globalData.userInfo.login = true
  61. this.initIndexData()
  62. } else {
  63. if (resIsNeedPhone) { // true 代表需要手机号授权
  64. that.setData({
  65. phoneAuthShow: true
  66. })
  67. } else {
  68. if (resHeadImg == '') { // 需要授权头像
  69. that.setData({
  70. userAuthShow: true
  71. })
  72. } else {
  73. app.globalData.userInfo.login = true
  74. }
  75. }
  76. }
  77. },
  78. // 获取本地存储你的头像 昵称 手机号 以及 accessToken 信息
  79. appLoginFn () {
  80. console.log('使用已存在的accessToekn登录')
  81. this.initIndexData()
  82. },
  83. // 使用wx.login => code => accessToken
  84. appCodeLoginFn () {
  85. var that = this
  86. wx.login({
  87. success (loginCode) {
  88. if (loginCode.code) {
  89. that.ittLoginFn(loginCode.code)
  90. }
  91. }
  92. })
  93. },
  94. // 登录函数
  95. ittLoginFn (code) {
  96. var that = this
  97. var data = {
  98. code: code
  99. }
  100. wx.showLoading({
  101. title: '登录中...',
  102. mask: true
  103. })
  104. ittLogin(data).then(res => {
  105. const response = res.data
  106. const accessToken = response.accessToken
  107. const isNeedPhone = response.isNeedPhone
  108. const isNeedHeadImg = response.headImg ? true : false
  109. const headImg = response.headImg
  110. const userName = response.userName
  111. const phoneNumber = response.phoneNumber
  112. app.globalData.accessToken = accessToken
  113. app.globalData.isNeedPhone = isNeedPhone
  114. app.globalData.isNeedHeadImg = isNeedHeadImg
  115. app.globalData.userInfo.headImg= headImg
  116. app.globalData.userInfo.userName = userName
  117. app.globalData.userInfo.phoneNumber = phoneNumber
  118. wx.setStorageSync('accessToken', accessToken)
  119. wx.setStorageSync('isNeedPhone', isNeedPhone)
  120. wx.setStorageSync('isNeedHeadImg', isNeedHeadImg)
  121. wx.setStorageSync('headImg', headImg)
  122. wx.setStorageSync('userName', userName)
  123. wx.setStorageSync('phoneNumber', phoneNumber)
  124. wx.hideLoading()
  125. that.handleAuth()
  126. }).catch(e => {
  127. wx.hideLoading()
  128. })
  129. },
  130. // 拉起手机号授权弹窗
  131. getPhoneNumber (e) {
  132. var phonePermission = e.detail.errMsg.split(':')[1]
  133. if (phonePermission == 'ok') {
  134. var phoneCode = e.detail.code
  135. this.bindMobileFn(phoneCode)
  136. } else {
  137. wx.showToast({
  138. title: '授权失败',
  139. icon: 'error'
  140. })
  141. }
  142. },
  143. // 绑定手机号
  144. bindMobileFn (phoneCode) {
  145. var that = this
  146. var data = {
  147. mobileCode: phoneCode
  148. }
  149. wx.showLoading({
  150. title: '加载中...',
  151. mask: true
  152. })
  153. bindMobile(data).then(res => {
  154. wx.hideLoading()
  155. const resPhoneNumber = res.data
  156. app.globalData.userInfo.phoneNumber = resPhoneNumber
  157. wx.setStorageSync('phoneNumber', resPhoneNumber)
  158. that.setData({
  159. phoneAuthShow: false
  160. })
  161. if (app.globalData.userInfo.headImg == '') {
  162. // 需要授权头像
  163. that.setData({
  164. userAuthShow: true
  165. })
  166. } else {
  167. app.globalData.userInfo.login = true
  168. }
  169. }).catch(e => {
  170. wx.hideLoading()
  171. wx.showModal({
  172. content: e,
  173. confirmColor: '#333',
  174. showCancel: false
  175. })
  176. })
  177. },
  178. // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
  179. getUserinfo () {
  180. var that = this
  181. wx.getUserProfile({
  182. desc: '用于获取用户权益'
  183. }).then(res => {
  184. const _avatarUrl = res.userInfo.avatarUrl
  185. const _nickName = res.userInfo.nickName
  186. that.setData({
  187. avatarUrl: _avatarUrl,
  188. nickname: _nickName
  189. })
  190. that.handleConfirmNickname()
  191. }).catch(e => {
  192. wx.showToast({
  193. title: '授权失败',
  194. icon: 'error'
  195. })
  196. })
  197. },
  198. // 提交用户头像和昵称到后台
  199. handleConfirmNickname () {
  200. var that = this
  201. var data = {
  202. avatarUrl: that.data.avatarUrl,
  203. nickName: that.data.nickname
  204. }
  205. wx.showLoading({
  206. title: '加载中...',
  207. mask: true
  208. })
  209. bindBaseInfo(data).then(res => {
  210. const _headImg = that.data.confirmAvatarUrl
  211. const _userName = that.data.confirmNickname
  212. app.globalData.userInfo.login = true
  213. app.globalData.userInfo.headImg = _headImg
  214. app.globalData.userInfo.userName = _userName
  215. wx.setStorageSync('headImg', _headImg)
  216. wx.setStorageSync('userName', _userName)
  217. that.setData({
  218. userAuthShow: false
  219. })
  220. that.initIndexData()
  221. wx.hideLoading()
  222. }).catch(e => {
  223. wx.hideLoading()
  224. wx.showModal({
  225. content: e,
  226. confirmColor: '#333',
  227. showCancel: false
  228. })
  229. })
  230. },
  231. /* onPullDownRefresh () {
  232. wx.vibrateShort({
  233. type: 'medium'
  234. })
  235. this.initIndexData(true)
  236. }, */
  237. onShareAppMessage () {
  238. return {
  239. title: 'ITTHealth',
  240. path: '/pages/index/index'
  241. }
  242. },
  243. // 获取首页信息 档案 剩余次数
  244. initIndexData (pullDownRefresh = false) {
  245. var that = this
  246. wx.showLoading({
  247. title: '加载中...',
  248. mask: true
  249. })
  250. homePage({}).then(hoemRes => {
  251. wx.hideLoading()
  252. if (pullDownRefresh) {
  253. wx.stopPullDownRefresh()
  254. }
  255. var response = hoemRes.data.documentVos || []
  256. response.map(item => {
  257. item.birthDay = item.birthday.split(' ')[0]
  258. })
  259. that.setData({
  260. useNumber: hoemRes.data.useNumber,
  261. documentVos: response
  262. })
  263. }).catch(e => {
  264. wx.hideLoading()
  265. wx.showModal({
  266. content: e,
  267. confirmColor: '#333',
  268. showCancel: false
  269. })
  270. })
  271. },
  272. /**
  273. * 处理套餐 购买 or 预约
  274. * useNumber: 0-购买 1-预约
  275. * **/
  276. handlePackage () {
  277. var type = this.data.useNumber > 0 ? 1 : 0
  278. switch (type) {
  279. case 0:
  280. wx.navigateTo({
  281. url: '/pages/buy/buy?from=index',
  282. })
  283. break;
  284. case 1:
  285. this.appointmentAuth()
  286. break;
  287. }
  288. },
  289. // 预约前判断是否存在一位检测人
  290. appointmentAuth () {
  291. if (this.data.documentVos.length == 0) {
  292. wx.showModal({
  293. content: '预约用户前需要先添加检测人员信息',
  294. cancelColor: '#666',
  295. confirmColor: '#333',
  296. success (res) {
  297. if (res.confirm) {
  298. wx.navigateTo({
  299. url: '/pages/createFile/createFile?from=index'
  300. })
  301. }
  302. }
  303. })
  304. } else {
  305. wx.navigateTo({
  306. url: '/pages/appointment/appointment?from=index',
  307. })
  308. }
  309. },
  310. // 点击全部档案
  311. handleAllFile () {
  312. wx.navigateTo({
  313. url: '/pages/myFile/myFile',
  314. })
  315. },
  316. // 点击查看报告
  317. handleRepoetDetail (e) {
  318. var reportid = e.currentTarget.dataset.reportid
  319. wx.navigateTo({
  320. url: '/pages/reportDetail/reportDetail?reportid=' + reportid
  321. })
  322. },
  323. // 添加检测人
  324. handleAddCheck () {
  325. wx.navigateTo({
  326. url: '/pages/createFile/createFile?form=index',
  327. })
  328. },
  329. /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */
  330. handlActions () {
  331. if (this.data.useNumber == 0) {
  332. wx.showModal({
  333. title: '去充值',
  334. cancelColor: '#666',
  335. cancelText: '激活卡',
  336. confirmText: '去充值',
  337. confirmColor: '#333',
  338. success (res) {
  339. if (res.confirm) {
  340. wx.navigateTo({
  341. url: '/pages/buy/buy?from=index',
  342. })
  343. } else if (res.cancel) {
  344. wx.navigateTo({
  345. url: '/pages/exchange/exchange',
  346. })
  347. }
  348. }
  349. })
  350. } else {
  351. this.appointmentAuth()
  352. }
  353. },
  354. handleShowQRCode (e) {
  355. var that = this
  356. var id = e.currentTarget.dataset.id
  357. var realname = e.currentTarget.dataset.realname
  358. this.setData({
  359. qrUserName: realname
  360. })
  361. wx.showLoading({
  362. title: '生成中...'
  363. })
  364. createQRcode({ documentId: id }).then(res => {
  365. wx.hideLoading()
  366. that.setData({
  367. QRCodeBase64: that.data.baseStr + res.data,
  368. qrcodeDialog: true
  369. })
  370. }).catch(e => {
  371. wx.hideLoading()
  372. wx.showModal({
  373. content: e,
  374. confirmColor: '#333',
  375. showCancel: false
  376. })
  377. })
  378. },
  379. handleCloseQRCode (e) {
  380. this.setData({
  381. qrcodeDialog: false,
  382. QRCodeBase64: ''
  383. })
  384. }
  385. })