my.js 7.5 KB

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