index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. userInfo: {},
  7. waterInfo: {},
  8. onQuery: true,
  9. unAuth:false,
  10. items: [{
  11. id: 'realtimewater',
  12. label: '用水量',
  13. icon: '/static/img/icon-water.png',
  14. url: '?period=7',
  15. sonlabel:'近期用水量'
  16. }, {
  17. id: 'waterabnormal',
  18. label: '异常预警',
  19. icon: '/static/img/icon_water_abnormal.png',
  20. sonlabel:'用水异常提醒'
  21. }
  22. ],
  23. itemsrow:[
  24. {
  25. id: 'wateranalysis',
  26. label: '用水趋势',
  27. icon: '/static/img/icon_water_analysis.png',
  28. sonlabel:'年度用水趋势'
  29. }, {
  30. id: 'customermanage',
  31. label: '户号管理',
  32. icon: '/static/img/btn_hubiao1.png',
  33. sonlabel:'绑定户号'
  34. }
  35. ],
  36. weixinLogin: false,
  37. },
  38. getPhoneNumber(e) {
  39. let that = this;
  40. if (e.detail.encryptedData) {
  41. const {
  42. iv,
  43. encryptedData
  44. } = e.detail;
  45. const {
  46. param,
  47. item = {}
  48. } = e.currentTarget.dataset;
  49. wx.login({
  50. success: res => {
  51. const {
  52. code
  53. } = res;
  54. app.$http.postForm('/appletUser/bindingMobile', {
  55. code,
  56. encryptedData,
  57. iv
  58. }).then(({
  59. status,
  60. data = {}
  61. }) => {
  62. if (status === 0) {
  63. // 绑定成功
  64. if (this.data.userInfo.deviceId) {
  65. wx.navigateTo({
  66. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  67. })
  68. }
  69. wx.showToast({
  70. title: '授权成功',
  71. icon: 'none',
  72. duration: 2000
  73. })
  74. that.setData({
  75. weixinLogin:true,
  76. onQuery:false
  77. })
  78. console.log("weixinLogin:"+that.data.weixinLogin)
  79. console.log("onQuery:"+that.data.onQuery)
  80. return;
  81. }
  82. // 绑定成功未注册
  83. else {
  84. wx.showToast({
  85. title: '您尚未注册,请到平台端开通账号后重试',
  86. icon: 'none',
  87. duration: 2000
  88. })
  89. return;
  90. }
  91. })
  92. }
  93. })
  94. }
  95. else{
  96. wx.showToast({
  97. title: '您尚未注册,请到平台端开通账号后重试',
  98. icon: 'none',
  99. duration: 2000
  100. })
  101. return;
  102. }
  103. },
  104. gotopage(e) {
  105. const {
  106. param,
  107. item = {}
  108. } = e.currentTarget.dataset;
  109. if (!this.data.weixinLogin) {
  110. wx.showToast({
  111. title: '请您先进行授权',
  112. icon: 'none',
  113. duration: 2000
  114. })
  115. return;
  116. }
  117. console.log("app的值",app)
  118. if (!this.data.userInfo.userNumber && param != 'customermanage' && item.id != 'customermanage') {
  119. wx.showToast({
  120. title: '请先绑定你的户号',
  121. icon: 'none',
  122. duration: 2000
  123. })
  124. return;
  125. }
  126. wx.navigateTo({
  127. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  128. })
  129. },
  130. getAccountInfo() {
  131. let _self=this
  132. this.setData({
  133. onQuery:true,
  134. unAuth:false
  135. })
  136. app.$util.selfUserInfo(app.globalData, true).then((data) => {
  137. if (data) {
  138. this.setData({
  139. userInfo: data,
  140. waterInfo: data.waterStages
  141. })
  142. _self.setData({
  143. onQuery:false
  144. })
  145. console.log("_self.onQuery:" + _self.data.onQuery)
  146. } else {
  147. this.setData({
  148. userInfo: {},
  149. waterInfo: {}
  150. })
  151. }
  152. }).catch(() => {
  153. _self.setData({
  154. onQuery:false
  155. })
  156. })
  157. },
  158. // 获取用户注册状态 /user/getUser
  159. isRegister(){
  160. let that=this;
  161. that.setData({unAuth:false})
  162. app.$http.postForm('/appletUser/getUser').then(({ status, msg, data }) => {
  163. // 已授权但客户不存在
  164. // 未授权
  165. if(status===-602){
  166. that.setData({
  167. weixinLogin:false
  168. })
  169. }else if(status===0){
  170. that.setData({
  171. weixinLogin:true
  172. })
  173. }
  174. else if(status===-406)
  175. {
  176. that.setData({
  177. weixinLogin:false,
  178. unAuth:true
  179. })
  180. wx.showToast({
  181. title: '您尚未注册认证,请到平台端开通账号后重试',
  182. icon: 'none',
  183. duration: 2000
  184. })
  185. return;
  186. }
  187. })
  188. },
  189. onShow() {
  190. let _self=this
  191. this.getAccountInfo();
  192. this.isRegister()
  193. },
  194. toLogin(msg) {
  195. this.popup && this.popup.setData({
  196. type: 'error',
  197. title: '提示',
  198. content: msg,
  199. showCancel: false
  200. });
  201. this.popup.show();
  202. },
  203. cancelEvent: function () {
  204. this.popup.close();
  205. },
  206. confirmEvent: function () {
  207. this.getAccountInfo(true);
  208. },
  209. /**
  210. * 生命周期函数--监听页面初次渲染完成
  211. */
  212. onReady: function () {
  213. //获得popup组件
  214. this.popup = this.selectComponent("#popup");
  215. },
  216. })