my.js 7.6 KB

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