my.js 8.1 KB

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