微信小程序
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.

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