my.js 7.0 KB

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