my.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. } else {
  104. if (res.data.isNeedPhone) { // true 代表需要手机号授权
  105. that.setData({
  106. isNeedPhoneBtn: true
  107. })
  108. } else {
  109. if (res.data.headImg == '') {
  110. // 需要授权头像
  111. that.setData({
  112. overshow: true,
  113. phone: res.data,
  114. isNeedPhoneBtn: false,
  115. isNeedHeadImgBtn: true
  116. })
  117. } else {
  118. that.setData({
  119. confirmAvatarUrl: app.globalData.userInfo.headImg,
  120. confirmNickname: app.globalData.userInfo.userName,
  121. })
  122. }
  123. }
  124. }
  125. }).catch(e => {
  126. wx.hideLoading()
  127. })
  128. }
  129. }
  130. })
  131. },
  132. onChooseAvatar(e) {
  133. const { avatarUrl } = e.detail
  134. this.setData({
  135. avatarUrl,
  136. })
  137. },
  138. // 拉起用户头像和昵称
  139. getNickName () {
  140. if (!this.data.pageLogin) {
  141. this.setData({
  142. overshow: true
  143. })
  144. }
  145. },
  146. // 拉起手机号授权弹窗
  147. getPhoneNumber (e) {
  148. var that = this
  149. var phonePermission = e.detail.errMsg.split(':')[1]
  150. if (phonePermission == 'ok') {
  151. var phoneCode = e.detail.code
  152. that.bindMobileFn(phoneCode)
  153. } else {
  154. wx.showToast({
  155. title: '手机号授权失败',
  156. icon: 'error'
  157. })
  158. }
  159. },
  160. // 绑定手机号
  161. bindMobileFn (phoneCode) {
  162. var that = this
  163. var data = {
  164. mobileCode: phoneCode
  165. }
  166. wx.showLoading({
  167. title: '加载中...',
  168. mask: true
  169. })
  170. bindMobile(data).then(res => {
  171. wx.hideLoading()
  172. app.globalData.userInfo.phoneNumber = res.data
  173. if (app.globalData.userInfo.headImg == '') {
  174. // 需要授权头像
  175. that.setData({
  176. overshow: true,
  177. phone: res.data,
  178. isNeedPhoneBtn: false,
  179. isNeedHeadImgBtn: true
  180. })
  181. } else {
  182. that.setData({
  183. confirmAvatarUrl: app.globalData.userInfo.headImg,
  184. confirmNickname: app.globalData.userInfo.userName,
  185. })
  186. app.globalData.userInfo.login = true
  187. }
  188. }).catch(e => {
  189. wx.hideLoading()
  190. wx.showModal({
  191. content: e,
  192. confirmColor: '#333',
  193. showCancel: false
  194. })
  195. })
  196. },
  197. bindNickNameInput (e) {
  198. this.setData({
  199. nickname: e.detail.value
  200. })
  201. },
  202. // 绑定用户头像和昵称
  203. handleConfirmNickname () {
  204. console.log(that.data.avatarUrl, that.data.nickname);
  205. var that = this
  206. if (that.data.avatarUrl == '') {
  207. wx.showToast({
  208. title: '请选择头像',
  209. icon: 'error'
  210. })
  211. return
  212. }
  213. if (that.data.nickname == '') {
  214. wx.showToast({
  215. title: '请输入昵称',
  216. icon: 'error'
  217. })
  218. return
  219. }
  220. var data = {
  221. avatarUrl: that.data.avatarUrl,
  222. nickName: that.data.nickname
  223. }
  224. wx.showLoading({
  225. title: '加载中...',
  226. mask: true
  227. })
  228. bindBaseInfo(data).then(res => {
  229. wx.hideLoading()
  230. app.globalData.userInfo.login = true
  231. that.setData({
  232. pageLogin: true,
  233. overshow: false,
  234. confirmAvatarUrl: that.data.avatarUrl,
  235. confirmNickname: that.data.nickname
  236. })
  237. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  238. app.globalData.userInfo.userName = that.data.confirmNickname
  239. }).catch(e => {
  240. wx.hideLoading()
  241. wx.showModal({
  242. content: e,
  243. confirmColor: '#333',
  244. showCancel: false
  245. })
  246. })
  247. },
  248. /**
  249. * 生命周期函数--监听页面显示
  250. */
  251. onShow() {
  252. this.setData({
  253. pageLogin: app.globalData.userInfo.login
  254. })
  255. },
  256. // 路由跳转
  257. handleNavTo (e) {
  258. var url = e.currentTarget.dataset.url
  259. this.authNavTo(url)
  260. },
  261. handleAppoint () {
  262. this.authNavTo('/pages/appointment/appointment')
  263. },
  264. /**
  265. * 路由拦截
  266. * **/
  267. authNavTo (url) {
  268. if (itt.loginAuth()) {
  269. if (url.indexOf('/report') !== -1) {
  270. app.globalData.selectedInex = 1
  271. wx.switchTab({
  272. url: url,
  273. })
  274. } else {
  275. wx.navigateTo({
  276. url: url,
  277. })
  278. }
  279. } else {
  280. itt.errorToast('请先授权登录')
  281. }
  282. }
  283. })