微信小程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

244 lignes
7.5 KiB

  1. // pages/index/index.js
  2. const APP = getApp();
  3. import * as UTIL from '../../../utils/util.js';
  4. import * as API from '../../../utils/API.js';
  5. import * as STORAGE from '../../../utils/storage'
  6. Page({
  7. data: {
  8. isIPhoneX:false,
  9. isIPX: APP.globalData.isIPX,
  10. privacyCheck:false, //用户协议
  11. showDialog:false,
  12. nowDress:'',
  13. showPhoneBtn:false,
  14. showPhoneBtnDisabled: false
  15. },
  16. onShow: function (options) {
  17. // this.setData({
  18. // isIPhoneX:UTIL.isIPhoneX()
  19. // })
  20. console.log('11111111');
  21. var that = this;
  22. let url = wx.getStorageSync('dressCode')
  23. that.setData({
  24. nowDress: wx.getStorageSync('dressName')
  25. })
  26. console.log(getApp().globalData.APIURL);
  27. if (url == '' || url == undefined || url == null) {
  28. wx.redirectTo({
  29. url: '../region/region',
  30. })
  31. }else{
  32. //获取code
  33. UTIL.getCOdeFromWX({
  34. complate: (code) => {
  35. //获取openId
  36. //console.log('aaaa');
  37. that.getOpenIdFromFW(code);
  38. }
  39. });
  40. }
  41. // this.automaticLogin()
  42. //this.getUserInfo()
  43. //this.getPhoneNumber()
  44. },
  45. goRegion(){
  46. wx.navigateTo({
  47. url: '/pages/user/region/region',
  48. })
  49. },
  50. /**
  51. * 从服务端获取openId
  52. */
  53. getOpenIdFromFW(code) {
  54. let sendData = {
  55. code: code
  56. }
  57. //console.log(wx.getStorageSync('dressCode')+API.URL_GET_OPENID);
  58. UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
  59. success: (res) => {
  60. //console.log(res);
  61. if (res.code == API.SUCCESS_CODE) {
  62. // UTIL.showToastNoneIcon("openId:" + res._data.openid);
  63. if(res.token){
  64. wx.setStorageSync('token', res.token);
  65. wx.setStorageSync('openId', res.data.openId);
  66. wx.setStorageSync('sessionKey', res.data.sessionKey);
  67. getApp().globalData.userInfo.token = res.token;
  68. }else{
  69. wx.setStorageSync('openId', res.data.openId);
  70. wx.setStorageSync('sessionKey', res.data.sessionKey);
  71. this.setData({showPhoneBtn:true})
  72. }
  73. } else {
  74. //未获取到openId
  75. // wx.setStorageSync('openId', res.data.openId);
  76. // wx.setStorageSync('sessionKey', res.data.sessionKey);
  77. this.setData({showPhoneBtn:true})
  78. }
  79. }
  80. })
  81. },
  82. goIndex(){
  83. if(this.data.privacyCheck == false){
  84. APP.showToast("请阅读并同意【服务须知】!");
  85. return;
  86. }
  87. wx.reLaunch({
  88. url: '/pages/index/index',
  89. })
  90. },
  91. //自动登录
  92. automaticLogin(){
  93. let automatic = STORAGE.getToken();
  94. let getOpenId = STORAGE.getOpenId();
  95. if(automatic == '' && getOpenId ==''){
  96. //console.log(1);
  97. UTIL.getCOdeFromWX({
  98. complate: (code) => {
  99. let sendData = {
  100. code: code
  101. }
  102. UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
  103. success: (res) => {
  104. //console.log(res);
  105. if (res.code == API.SUCCESS_CODE) {
  106. wx.reLaunch({
  107. url: '/pages/index/index',
  108. })
  109. } else{
  110. this.getPhoneNumber()
  111. }
  112. }
  113. })
  114. }
  115. });
  116. }else{
  117. // wx.navigateTo({
  118. // url: '/pages/index/index',
  119. // })
  120. }
  121. },
  122. //用户隐私协议选项
  123. checkboxChange: function(res) {
  124. let checkStatus = false;
  125. if(res.detail.value.length!=0){
  126. checkStatus = true;
  127. }else{
  128. checkStatus = false;
  129. }
  130. this.setData({
  131. privacyCheck:checkStatus
  132. })
  133. },
  134. /* 获取用户信息*/
  135. getUserInfo(){
  136. UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
  137. success: (res) => {
  138. if (res.code == API.SUCCESS_CODE) {
  139. this.setData({userInfoObj:res.user})
  140. }
  141. }
  142. })
  143. },
  144. //微信一键登录授权
  145. getPhoneNumber: function(res) {
  146. let that = this;
  147. let {
  148. detail
  149. } = res;
  150. if (!detail.encryptedData) {
  151. //允许授权
  152. APP.showToast("未获取到手机号码,登录失败!");
  153. return;
  154. }else if(this.data.privacyCheck == false){
  155. APP.showToast("请阅读并同意【服务须知】!");
  156. return;
  157. }
  158. //console.log(detail)
  159. let sendData = {
  160. sessionKey:wx.getStorageSync('sessionKey'),
  161. iv:detail.iv,
  162. encryptedData:detail.encryptedData
  163. }
  164. UTIL.httpRequestNoneDetal(API.URL_POST_DECRYPTEDWXDATA, sendData, "POST", {
  165. success: (res) => {
  166. //console.log(res)
  167. if (res.code == API.SUCCESS_CODE) {
  168. let phoneNumber = res.data.phoneNumber;
  169. //微信手机号码绑定
  170. that.wxUserBand(phoneNumber)
  171. // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
  172. } else {
  173. //待删
  174. UTIL.showToastNoneIcon(res.msg)
  175. this.setData({showPhoneBtnDisabled: true})
  176. }
  177. },
  178. fail: (res) => {
  179. //console.log("fail");
  180. UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  181. this.setData({showPhoneBtnDisabled: true})
  182. }
  183. })
  184. // UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{
  185. // success: (res) => {
  186. // console.log(res);
  187. // // if (res.code == API.SUCCESS_CODE) {
  188. // // let phoneNumber = res.data.phoneNumber;
  189. // // //微信手机号码绑定
  190. // // that.wxUserBand(phoneNumber)
  191. // // // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
  192. // // } else {
  193. // // //待删
  194. // // UTIL.showToastNoneIcon(res.msg)
  195. // // }
  196. // },
  197. // fail: (res) => {
  198. // console.log("fail");
  199. // UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  200. // }
  201. // });
  202. },
  203. wxUserBand(phone){
  204. let sendData = {
  205. openId:STORAGE.getOpenId(),
  206. phonenumber:phone
  207. }
  208. //console.log(STORAGE.getOpenId());
  209. UTIL.httpRequestNoneDetal(API.URL_POST_USERBAND, sendData,"POST",{
  210. success: (res) => {
  211. if (res.code == API.SUCCESS_CODE) {
  212. wx.setStorageSync('token', res.token);
  213. wx.reLaunch({
  214. // url: '/pages/user/region/region',
  215. url: '/pages/index/index',
  216. })
  217. }else{
  218. UTIL.showToastNoneIcon(res.msg)
  219. }
  220. },
  221. fail: (res) => {
  222. UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
  223. }
  224. });
  225. //
  226. },
  227. //打开协议窗口
  228. openDialog(){
  229. this.setData({showDialog:true})
  230. },
  231. agree(){
  232. this.setData({
  233. privacyCheck:true
  234. })
  235. },
  236. goPhoneLogin(){
  237. wx.navigateTo({
  238. url: '/pages/phoneLogin',
  239. })
  240. }
  241. })