微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.1 KiB

  1. // pages/index/index.js
  2. import * as UTIL from '../../../utils/util.js';
  3. import * as API from '../../../utils/API.js';
  4. import * as STORAGE from '../../../utils/storage'
  5. const APP = getApp();
  6. Page({
  7. data: {
  8. isIPhoneX:false,
  9. privacyCheck:true, //用户协议
  10. showDialog:false
  11. },
  12. onLoad: function (options) {
  13. this.setData({
  14. isIPhoneX:UTIL.isIPhoneX()
  15. })
  16. this.automaticLogin()
  17. //this.getUserInfo()
  18. //this.getPhoneNumber()
  19. },
  20. //自动登录
  21. automaticLogin(){
  22. let automatic = STORAGE.getToken();
  23. let getOpenId = STORAGE.getOpenId();
  24. if(automatic == '' && getOpenId ==''){
  25. console.log(1);
  26. UTIL.getCOdeFromWX({
  27. complate: (code) => {
  28. let sendData = {
  29. code: code
  30. }
  31. UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
  32. success: (res) => {
  33. console.log(res);
  34. if (res.code == API.SUCCESS_CODE) {
  35. wx.navigateTo({
  36. url: '/pages/index/index',
  37. })
  38. } else{
  39. this.getPhoneNumber()
  40. }
  41. }
  42. })
  43. }
  44. });
  45. }else{
  46. // wx.navigateTo({
  47. // url: '/pages/index/index',
  48. // })
  49. }
  50. },
  51. //用户隐私协议选项
  52. checkboxChange: function(res) {
  53. let checkStatus = false;
  54. if(res.detail.value.length!=0){
  55. checkStatus = true;
  56. }else{
  57. checkStatus = false;
  58. }
  59. this.setData({
  60. privacyCheck:checkStatus
  61. })
  62. },
  63. /* 获取用户信息*/
  64. getUserInfo(){
  65. UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
  66. success: (res) => {
  67. if (res.code == API.SUCCESS_CODE) {
  68. this.setData({userInfoObj:res.user})
  69. }
  70. }
  71. })
  72. },
  73. //微信一键登录授权
  74. getPhoneNumber: function(res) {
  75. let that = this;
  76. let {
  77. detail
  78. } = res;
  79. if (!detail.encryptedData) {
  80. //允许授权
  81. APP.showToast("未获取到手机号码,注册失败!");
  82. return;
  83. }else if(this.data.privacyCheck == false){
  84. APP.showToast("请阅读并同意用户协议和隐私政策!");
  85. return;
  86. }
  87. let sendData = {
  88. sessionKey:STORAGE.getSessionKey(),
  89. iv:detail.iv,
  90. encryptedData:detail.encryptedData
  91. }
  92. UTIL.httpRequestNoneDetal(API.URL_POST_DECRYPTEDWXDATA, sendData, "POST", {
  93. success: (res) => {
  94. console.log(res)
  95. if (res.code == API.SUCCESS_CODE) {
  96. let phoneNumber = res.data.phoneNumber;
  97. //微信手机号码绑定
  98. that.wxUserBand(phoneNumber)
  99. // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
  100. } else {
  101. //待删
  102. UTIL.showToastNoneIcon(res.msg)
  103. }
  104. },
  105. fail: (res) => {
  106. console.log("fail");
  107. UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  108. }
  109. })
  110. // UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{
  111. // success: (res) => {
  112. // console.log(res);
  113. // // if (res.code == API.SUCCESS_CODE) {
  114. // // let phoneNumber = res.data.phoneNumber;
  115. // // //微信手机号码绑定
  116. // // that.wxUserBand(phoneNumber)
  117. // // // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
  118. // // } else {
  119. // // //待删
  120. // // UTIL.showToastNoneIcon(res.msg)
  121. // // }
  122. // },
  123. // fail: (res) => {
  124. // console.log("fail");
  125. // UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  126. // }
  127. // });
  128. },
  129. wxUserBand(phone){
  130. let sendData = {
  131. openId:STORAGE.getOpenId(),
  132. phonenumber:phone
  133. }
  134. console.log(STORAGE.getOpenId());
  135. UTIL.httpRequestNoneDetal(API.URL_POST_USERBAND, sendData,"POST",{
  136. success: (res) => {
  137. if (res.code == API.SUCCESS_CODE) {
  138. wx.navigateTo({
  139. url: '/pages/index/index',
  140. })
  141. }else{
  142. UTIL.showToastNoneIcon(res.msg)
  143. }
  144. },
  145. fail: (res) => {
  146. UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  147. }
  148. });
  149. //
  150. },
  151. //打开协议窗口
  152. openDialog(){
  153. this.setData({showDialog:true})
  154. },
  155. agree(){
  156. this.setData({
  157. privacyCheck:true
  158. })
  159. }
  160. })