my.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // pages/my/my.js
  2. import itt from '../../utils/util'
  3. import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. overshow: false,
  11. pageLogin: false,
  12. showPhoneAuthBtn: true,
  13. showAvaNick: false,
  14. isNeedPhoneBtn: false, // 是否需要显示手机号授权
  15. isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权
  16. isRegister: false,
  17. avatarUrl: '',
  18. confirmAvatarUrl: '',
  19. confirmNickname: '点击获取头像昵称',
  20. nickname: '',
  21. phone: '',
  22. count: 0,
  23. gridList: [
  24. {
  25. id: 0,
  26. title: '我的地址',
  27. url: '/pages/address/address?form=my&back=0',
  28. imgUrl: '../../imaes/dizhi@2x.png'
  29. },
  30. {
  31. id: 1,
  32. title: '我的档案',
  33. url: '/pages/myFile/myFile?form=my',
  34. imgUrl: '../../imaes/dangan@2x.png'
  35. },
  36. {
  37. id: 2,
  38. title: '我的预约',
  39. url: '/pages/myAppointment/myAppointment?form=my',
  40. imgUrl: '../../imaes/yuyue@2x.png'
  41. },
  42. {
  43. id: 7,
  44. title: '实体卡兑换',
  45. url: '/pages/exchange/exchange?form=my',
  46. imgUrl: '../../imaes/chongzhi.png'
  47. },
  48. {
  49. id: 3,
  50. title: '我的记录',
  51. url: '/pages/myRecord/myRecord?form=my',
  52. imgUrl: '../../imaes/jilu@2x.png'
  53. },
  54. {
  55. id: 4,
  56. title: '我的报告',
  57. url: '/pages/report/report?form=my',
  58. imgUrl: '../../imaes/baogao2@2x.png'
  59. },
  60. {
  61. id: 5,
  62. title: '开通区域',
  63. url: '/pages/openArea/openArea?form=my',
  64. imgUrl: '../../imaes/quyu@2x.png'
  65. },
  66. {
  67. id: 6,
  68. title: '设置',
  69. url: '/pages/set/set?form=my',
  70. imgUrl: '../../imaes/shezhi@2x.png'
  71. }
  72. ]
  73. },
  74. /**
  75. * 生命周期函数--监听页面加载
  76. * 调用login接口,判断是否需要绑定手机号和头像昵称
  77. */
  78. onLoad(options) {
  79. app.globalData.selectedInex = 2
  80. this.userLogin()
  81. },
  82. // 通过 code 获取 token
  83. userLogin () {
  84. var that = this
  85. wx.login({
  86. success (loginCode) {
  87. if (loginCode.code) {
  88. var data = {
  89. code: loginCode.code
  90. }
  91. wx.showLoading({
  92. title: '登录中...',
  93. mask: true
  94. })
  95. ittLogin(data).then(res => {
  96. wx.hideLoading()
  97. app.globalData.accessToken = res.data.accessToken
  98. app.globalData.userInfo.headImg= res.data.headImg
  99. app.globalData.userInfo.userName = res.data.userName
  100. app.globalData.userInfo.phoneNumber = res.data.phoneNumber
  101. if (!res.data.isNeedPhone && res.data.headImg !== '') {
  102. app.globalData.userInfo.login = true
  103. that.setData({
  104. isNeedHeadImgBtn: true,
  105. confirmAvatarUrl: app.globalData.userInfo.headImg,
  106. confirmNickname: app.globalData.userInfo.userName,
  107. })
  108. } else {
  109. if (res.data.isNeedPhone) { // true 代表需要手机号授权
  110. that.setData({
  111. isNeedPhoneBtn: true
  112. })
  113. } else {
  114. if (res.data.headImg == '') {
  115. // 需要授权头像
  116. that.setData({
  117. overshow: true,
  118. phone: res.data,
  119. isNeedPhoneBtn: false,
  120. isNeedHeadImgBtn: true
  121. })
  122. } else {
  123. that.setData({
  124. isNeedHeadImgBtn: true,
  125. confirmAvatarUrl: app.globalData.userInfo.headImg,
  126. confirmNickname: app.globalData.userInfo.userName,
  127. })
  128. }
  129. }
  130. }
  131. }).catch(e => {
  132. wx.hideLoading()
  133. })
  134. }
  135. }
  136. })
  137. },
  138. onChooseAvatar(e) {
  139. const { avatarUrl } = e.detail
  140. this.setData({
  141. avatarUrl,
  142. })
  143. },
  144. // 拉起用户头像和昵称
  145. getNickName () {
  146. if (!this.data.pageLogin) {
  147. this.setData({
  148. overshow: true
  149. })
  150. }
  151. },
  152. // 拉起手机号授权弹窗
  153. getPhoneNumber (e) {
  154. var that = this
  155. var phonePermission = e.detail.errMsg.split(':')[1]
  156. if (phonePermission == 'ok') {
  157. var phoneCode = e.detail.code
  158. that.bindMobileFn(phoneCode)
  159. } else {
  160. wx.showToast({
  161. title: '手机号授权失败',
  162. icon: 'error'
  163. })
  164. }
  165. },
  166. // 绑定手机号
  167. bindMobileFn (phoneCode) {
  168. var that = this
  169. var data = {
  170. mobileCode: phoneCode
  171. }
  172. wx.showLoading({
  173. title: '加载中...',
  174. mask: true
  175. })
  176. bindMobile(data).then(res => {
  177. wx.hideLoading()
  178. app.globalData.userInfo.phoneNumber = res.data
  179. if (app.globalData.userInfo.headImg == '') {
  180. // 需要授权头像
  181. that.setData({
  182. overshow: true,
  183. phone: res.data,
  184. isNeedPhoneBtn: false,
  185. isNeedHeadImgBtn: true
  186. })
  187. } else {
  188. that.setData({
  189. isNeedHeadImgBtn: true,
  190. confirmAvatarUrl: app.globalData.userInfo.headImg,
  191. confirmNickname: app.globalData.userInfo.userName,
  192. })
  193. app.globalData.userInfo.login = true
  194. }
  195. }).catch(e => {
  196. wx.hideLoading()
  197. wx.showModal({
  198. content: e,
  199. confirmColor: '#333',
  200. showCancel: false
  201. })
  202. })
  203. },
  204. bindNickNameInput (e) {
  205. this.setData({
  206. nickname: e.detail.value
  207. })
  208. },
  209. // 绑定用户头像和昵称
  210. handleConfirmNickname () {
  211. var that = this
  212. if (that.data.avatarUrl == '') {
  213. wx.showToast({
  214. title: '请选择头像',
  215. icon: 'error'
  216. })
  217. return
  218. }
  219. if (that.data.nickname == '') {
  220. wx.showToast({
  221. title: '请输入昵称',
  222. icon: 'error'
  223. })
  224. return
  225. }
  226. var data = {
  227. avatarUrl: that.data.avatarUrl,
  228. nickName: that.data.nickname
  229. }
  230. wx.showLoading({
  231. title: '加载中...',
  232. mask: true
  233. })
  234. bindBaseInfo(data).then(res => {
  235. wx.hideLoading()
  236. app.globalData.userInfo.login = true
  237. that.setData({
  238. pageLogin: true,
  239. overshow: false,
  240. confirmAvatarUrl: that.data.avatarUrl,
  241. confirmNickname: that.data.nickname
  242. })
  243. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  244. app.globalData.userInfo.userName = that.data.confirmNickname
  245. }).catch(e => {
  246. wx.hideLoading()
  247. wx.showModal({
  248. content: e,
  249. confirmColor: '#333',
  250. showCancel: false
  251. })
  252. })
  253. },
  254. /**
  255. * 生命周期函数--监听页面显示
  256. */
  257. onShow() {
  258. this.setData({
  259. pageLogin: app.globalData.userInfo.login
  260. })
  261. },
  262. // 路由跳转
  263. handleNavTo (e) {
  264. var url = e.currentTarget.dataset.url
  265. this.authNavTo(url)
  266. },
  267. handleAppoint () {
  268. this.authNavTo('/pages/appointment/appointment')
  269. },
  270. /**
  271. * 路由拦截
  272. * **/
  273. authNavTo (url) {
  274. if (itt.loginAuth()) {
  275. if (url.indexOf('/report') !== -1) {
  276. app.globalData.selectedInex = 1
  277. wx.switchTab({
  278. url: url,
  279. })
  280. } else {
  281. wx.navigateTo({
  282. url: url,
  283. })
  284. }
  285. } else {
  286. itt.errorToast('请先授权登录')
  287. }
  288. }
  289. })