my.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // pages/my/my.js
  2. import itt from '../../utils/util'
  3. import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
  4. import { getAccountNmber } from '../../api/appointment'
  5. import { userUploadHeadImg } from '../../api/upload'
  6. import { documentList } from "../../api/document"
  7. const app = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. overshow: false,
  14. pageLogin: false,
  15. isRelogin: false, // 是否为设置退出登录重新登录
  16. showPhoneAuthBtn: true,
  17. showAvaNick: false,
  18. isNeedPhoneBtn: false, // 是否需要显示手机号授权
  19. isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权
  20. isRegister: false,
  21. avatarUrl: '',
  22. confirmAvatarUrl: '',
  23. confirmNickname: '',
  24. nickname: '',
  25. phone: '',
  26. count: 0,
  27. isFirstGetCount: true, // 是否为第一次获取检测机会的次数
  28. gridList: [
  29. {
  30. id: 0,
  31. title: '我的地址',
  32. url: '/pages/address/address?form=my&back=0',
  33. imgUrl: '../../imaes/dizhi@2x.png'
  34. },
  35. {
  36. id: 1,
  37. title: '我的档案',
  38. url: '/pages/myFile/myFile?form=my',
  39. imgUrl: '../../imaes/dangan@2x.png'
  40. },
  41. {
  42. id: 2,
  43. title: '我的预约',
  44. url: '/pages/myAppointment/myAppointment?form=my',
  45. imgUrl: '../../imaes/yuyue@2x.png'
  46. },
  47. {
  48. id: 7,
  49. title: '实体卡兑换',
  50. url: '/pages/exchange/exchange?form=my',
  51. imgUrl: '../../imaes/chongzhi.png'
  52. },
  53. {
  54. id: 3,
  55. title: '我的记录',
  56. url: '/pages/myRecord/myRecord?form=my',
  57. imgUrl: '../../imaes/jilu@2x.png'
  58. },
  59. {
  60. id: 4,
  61. title: '我的报告',
  62. url: '/pages/report/report?form=my',
  63. imgUrl: '../../imaes/baogao2@2x.png'
  64. },
  65. /* {
  66. id: 5,
  67. title: '开通区域',
  68. url: '/pages/openArea/openArea?form=my',
  69. imgUrl: '../../imaes/quyu@2x.png'
  70. }, */
  71. {
  72. id: 7,
  73. title: '设置',
  74. url: '/pages/set/set?form=my',
  75. imgUrl: '../../imaes/shezhi@2x.png'
  76. },
  77. /* {
  78. id: 7,
  79. title: '测试页面',
  80. url: '/pages/aatest/test',
  81. imgUrl: '../../imaes/shezhi@2x.png'
  82. } */
  83. ]
  84. },
  85. onShow () {
  86. console.log('my onshow ===========', this.data.isFirstGetCount);
  87. this.setData({
  88. pageLogin: app.globalData.userInfo.login
  89. })
  90. if (!this.data.isFirstGetCount) {
  91. this.getAccountNmberFn()
  92. }
  93. },
  94. /**
  95. * 生命周期函数--监听页面加载
  96. * 调用login接口,判断是否需要绑定手机号和头像昵称
  97. */
  98. onLoad(options) {
  99. app.globalData.selectedInex = 2
  100. this.userLogin()
  101. },
  102. // 获取用户档案信息
  103. getDocumentList () {
  104. var that = this
  105. wx.showLoading({
  106. title: '加载中...',
  107. mask: true
  108. })
  109. documentList({
  110. currentPage: 1
  111. }).then(res => {
  112. wx.hideLoading()
  113. const response = res.data.vos || []
  114. if (response.length == 0) {
  115. wx.showModal({
  116. content: '预约用户前需要先添加检测人员信息',
  117. cancelColor: '#666',
  118. confirmColor: '#333',
  119. success (res) {
  120. if (res.confirm) {
  121. wx.navigateTo({
  122. url: '/pages/createFile/createFile?from=index'
  123. })
  124. }
  125. }
  126. })
  127. } else {
  128. that.authNavTo('/pages/appointment/appointment')
  129. }
  130. }).catch(e => {
  131. wx.hideLoading()
  132. wx.showModal({
  133. content: e,
  134. confirmColor: '#333',
  135. showCancel: false
  136. })
  137. })
  138. },
  139. // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
  140. getUserinfo () {
  141. var that = this
  142. wx.getUserProfile({
  143. desc: '用于获取用户权益'
  144. }).then(res => {
  145. const _avatarUrl = res.userInfo.avatarUrl
  146. const _nickName = res.userInfo.nickName
  147. that.setData({
  148. avatarUrl: _avatarUrl,
  149. nickname: _nickName
  150. })
  151. that.handleConfirmNickname()
  152. }).catch(e => {
  153. wx.showToast({
  154. title: '授权失败',
  155. icon: 'error'
  156. })
  157. })
  158. },
  159. // 退出登录后重新登录
  160. handleReLogin () {
  161. this.userLogin()
  162. },
  163. // 通过 code 获取 token
  164. userLogin () {
  165. var that = this
  166. wx.login({
  167. success (loginCode) {
  168. if (loginCode.code) {
  169. var data = {
  170. code: loginCode.code
  171. }
  172. wx.showLoading({
  173. title: '登录中...',
  174. mask: true
  175. })
  176. ittLogin(data).then(res => {
  177. wx.hideLoading()
  178. app.globalData.accessToken = res.data.accessToken
  179. app.globalData.userInfo.headImg= res.data.headImg
  180. app.globalData.userInfo.userName = res.data.userName
  181. app.globalData.userInfo.phoneNumber = res.data.phoneNumber
  182. const resIsNeedPhone = res.data.isNeedPhone
  183. const resHeadImg = res.data.headImg
  184. that.getAccountNmberFn()
  185. if (!resIsNeedPhone && resHeadImg !== '') {
  186. app.globalData.userInfo.login = true
  187. that.setData({
  188. isNeedHeadImgBtn: true,
  189. confirmAvatarUrl: app.globalData.userInfo.headImg,
  190. confirmNickname: app.globalData.userInfo.userName,
  191. })
  192. } else {
  193. if (resIsNeedPhone) { // true 代表需要手机号授权
  194. that.setData({
  195. isNeedPhoneBtn: true
  196. })
  197. } else {
  198. if (resHeadImg == '') {
  199. // 需要授权头像
  200. that.setData({
  201. overshow: true,
  202. phone: res.data,
  203. isNeedPhoneBtn: false,
  204. isNeedHeadImgBtn: true
  205. })
  206. } else {
  207. that.setData({
  208. isNeedHeadImgBtn: true,
  209. confirmAvatarUrl: app.globalData.userInfo.headImg,
  210. confirmNickname: app.globalData.userInfo.userName,
  211. })
  212. }
  213. }
  214. }
  215. }).catch(e => {
  216. wx.hideLoading()
  217. })
  218. }
  219. }
  220. })
  221. },
  222. // 获取用户剩余检测机会
  223. getAccountNmberFn () {
  224. var that = this
  225. wx.showNavigationBarLoading()
  226. getAccountNmber({}).then(res => {
  227. wx.hideNavigationBarLoading()
  228. that.setData({
  229. count: res.data,
  230. isFirstGetCount: false
  231. })
  232. })
  233. },
  234. // 用户本地选择图片头像
  235. onChooseAvatar(e) {
  236. const { avatarUrl } = e.detail
  237. this.uploadImg(avatarUrl)
  238. },
  239. // 上传用户头像文件到后台
  240. uploadImg (avatarUrl) {
  241. var that = this
  242. var data = {
  243. filePath: avatarUrl
  244. }
  245. wx.showLoading({
  246. title: '上传中...',
  247. mask: true
  248. })
  249. userUploadHeadImg(data).then(res => {
  250. wx.hideLoading()
  251. var response = JSON.parse(res)
  252. if (response.code != '000') {
  253. wx.showToast({
  254. title: '头像上传失败',
  255. icon: 'error'
  256. })
  257. } else {
  258. that.setData({
  259. avatarUrl: response.data
  260. })
  261. }
  262. }).catch(e => {
  263. wx.hideLoading()
  264. wx.showModal({
  265. content: e,
  266. confirmColor: '#333',
  267. showCancel: false
  268. })
  269. })
  270. },
  271. // 拉起手机号授权弹窗
  272. getPhoneNumber (e) {
  273. var that = this
  274. var phonePermission = e.detail.errMsg.split(':')[1]
  275. if (phonePermission == 'ok') {
  276. var phoneCode = e.detail.code
  277. that.bindMobileFn(phoneCode)
  278. } else {
  279. wx.showToast({
  280. title: '授权失败',
  281. icon: 'error'
  282. })
  283. }
  284. },
  285. // 绑定手机号
  286. bindMobileFn (phoneCode) {
  287. var that = this
  288. var data = {
  289. mobileCode: phoneCode
  290. }
  291. wx.showLoading({
  292. title: '加载中...',
  293. mask: true
  294. })
  295. bindMobile(data).then(res => {
  296. wx.hideLoading()
  297. app.globalData.userInfo.phoneNumber = res.data
  298. if (app.globalData.userInfo.headImg == '') {
  299. // 需要授权头像
  300. that.setData({
  301. overshow: true,
  302. phone: res.data,
  303. isNeedPhoneBtn: false,
  304. isNeedHeadImgBtn: true
  305. })
  306. } else {
  307. that.setData({
  308. isNeedHeadImgBtn: true,
  309. confirmAvatarUrl: app.globalData.userInfo.headImg,
  310. confirmNickname: app.globalData.userInfo.userName,
  311. })
  312. app.globalData.userInfo.login = true
  313. }
  314. }).catch(e => {
  315. wx.hideLoading()
  316. wx.showModal({
  317. content: e,
  318. confirmColor: '#333',
  319. showCancel: false
  320. })
  321. })
  322. },
  323. // 提交用户头像和昵称到后台
  324. handleConfirmNickname () {
  325. var that = this
  326. if (that.data.avatarUrl == '') {
  327. wx.showToast({
  328. title: '请选择头像',
  329. icon: 'error'
  330. })
  331. return
  332. }
  333. if (that.data.nickname == '') {
  334. wx.showToast({
  335. title: '请输入昵称',
  336. icon: 'error'
  337. })
  338. return
  339. }
  340. var data = {
  341. avatarUrl: that.data.avatarUrl,
  342. nickName: that.data.nickname
  343. }
  344. wx.showLoading({
  345. title: '加载中...',
  346. mask: true
  347. })
  348. bindBaseInfo(data).then(res => {
  349. wx.hideLoading()
  350. app.globalData.userInfo.login = true
  351. that.setData({
  352. pageLogin: true,
  353. overshow: false,
  354. confirmAvatarUrl: that.data.avatarUrl,
  355. confirmNickname: that.data.nickname
  356. })
  357. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  358. app.globalData.userInfo.userName = that.data.confirmNickname
  359. }).catch(e => {
  360. wx.hideLoading()
  361. wx.showModal({
  362. content: e,
  363. confirmColor: '#333',
  364. showCancel: false
  365. })
  366. })
  367. },
  368. // 路由跳转
  369. handleNavTo (e) {
  370. var url = e.currentTarget.dataset.url
  371. this.authNavTo(url)
  372. },
  373. // 去充值
  374. handleCharge () {
  375. this.authNavTo('/pages/buy/buy?from=my')
  376. },
  377. // 立即预约
  378. handleAppoint () {
  379. this.getDocumentList()
  380. },
  381. /**
  382. * 路由拦截
  383. * **/
  384. authNavTo (url) {
  385. if (itt.loginAuth()) {
  386. if (url.indexOf('/report') !== -1) {
  387. app.globalData.selectedInex = 1
  388. wx.switchTab({
  389. url: url,
  390. })
  391. } else {
  392. wx.navigateTo({
  393. url: url,
  394. })
  395. }
  396. } else {
  397. itt.errorToast('请先授权登录')
  398. }
  399. }
  400. })