my.js 8.9 KB

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