东风启辰小程序端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

210 lines
6.4KB

  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var openid = wx.getStorageSync('openid') || "";
  6. if (!openid) {
  7. // 登录
  8. this.wxLogin();
  9. } else {
  10. this.globalData.openid = openid;
  11. }
  12. var userMobile = wx.getStorageSync('userMobile');
  13. if (userMobile){
  14. this.globalData.userMobile = userMobile;
  15. }
  16. var friendOpenid = wx.getStorageSync('friendOpenid');
  17. if (friendOpenid) {
  18. this.globalData.friendOpenid = friendOpenid;
  19. }
  20. var userInfoData = wx.getStorageSync("userInfoData");
  21. if (userInfoData){
  22. this.globalData.userInfoData = {};
  23. this.globalData.userInfoData.avatarUrl = userInfoData.avatarUrl;
  24. this.globalData.userInfoData.nickName = userInfoData.nickName;
  25. }
  26. },
  27. wxLogin() {
  28. wx.login({
  29. success: res => {
  30. if (res.code) {
  31. this.getOpenid(res.code);
  32. } else {
  33. console.log('登录失败!' + res.errMsg)
  34. }
  35. }
  36. })
  37. },
  38. globalData: {
  39. urlRoot: "https://dongfengqichen.jiyou-tech.com/",//测试接口根目录
  40. // urlRoot: "",//接口根目录
  41. urlStatic:"https://www.jiyou-tech.com/2020/496_qichen/static",//静态资源根目录
  42. openid: "",//OPENID
  43. friendOpenid:"",//推荐人的openid
  44. session_key: "",//session_key
  45. openidSuccessFuc: null,//方法回调
  46. nowPage:'1',//当前tabBar
  47. isRegister:false,//是否已注册
  48. userMobile:null,//用户手机号
  49. isFirstLogin:true,//是否为第一次登录
  50. indexData:{},//首页数据
  51. myCenterData:null,//个人中心数据
  52. userInfoData:null,
  53. certificationState:0,//1车主,2合伙人,3同事
  54. mobileData:null,
  55. },
  56. // 获取openId
  57. getOpenid: function (code) {
  58. wx.showLoading({
  59. title: '加载中',
  60. mask: true,
  61. })
  62. this.wxRequest(this.globalData.urlRoot + "wxInfo/getOpenid", { code: code }, res => {
  63. wx.hideLoading();
  64. console.log(res);
  65. if (res.code == 200) {
  66. wx.setStorageSync("openid", res.data.openid);
  67. wx.setStorageSync("session_key", res.data.session_key);
  68. this.globalData.openid = res.data.openid;
  69. this.globalData.session_key = res.data.session_key;
  70. if (this.globalData.openidSuccessFuc) {
  71. this.globalData.openidSuccessFuc();
  72. }
  73. if(this.globalData.mobileData){
  74. this.getMobile2(this.globalData.mobileData.encryptedData, this.globalData.mobileData.iv, this.globalData.mobileData.callback, this.globalData.mobileData.thisArg);
  75. }
  76. } else {
  77. wx.showToast({
  78. title: res.msg,
  79. icon: "none"
  80. })
  81. }
  82. }, this);
  83. },
  84. /**
  85. * wx.request
  86. */
  87. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  88. let that = this;
  89. var httpUrl = url;
  90. var str = "";
  91. var count = 0;
  92. for (let key in params) {
  93. if (count) {
  94. str += "&" + key + "=" + params[key];
  95. } else {
  96. str += key + "=" + params[key];
  97. }
  98. count++;
  99. }
  100. if (str) {
  101. httpUrl += "?" + str;
  102. }
  103. if (!methods) {
  104. methods = "GET";
  105. }
  106. if (methods == "POST") {
  107. wx.request({
  108. url: url,
  109. data: params,
  110. method: methods,
  111. header: {
  112. "content-type": "application/x-www-form-urlencoded",
  113. "OPENID": this.globalData.openid
  114. },
  115. success: function (re) {
  116. // if (re.data.code == -1002) {
  117. // wx.removeStorageSync('openid');
  118. // that.wxLogin();
  119. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  120. // }
  121. if (callback && thisArg) {
  122. callback.call(thisArg, re.data);
  123. }
  124. },
  125. fail: function (re) {
  126. wx.hideLoading();
  127. }
  128. })
  129. } else {
  130. wx.request({
  131. url: httpUrl,
  132. method: methods,
  133. header: {
  134. "OPENID": this.globalData.openid,
  135. },
  136. success: function (re) {
  137. if (re.data.code == -1002) {
  138. wx.removeStorageSync('openid');
  139. that.wxLogin();
  140. // that.wxRequest(url, params, callback, thisArg, methods, openid);
  141. }
  142. if (callback && thisArg) {
  143. callback.call(thisArg, re.data);
  144. }
  145. },
  146. fail: function (res) {
  147. wx.hideLoading();
  148. }
  149. })
  150. }
  151. },
  152. submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
  153. this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => { }, this, "POST")
  154. },
  155. codeVerify: function (card) {//身份证号码验证
  156. let rule = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
  157. return rule.test(card);
  158. },
  159. mobileVerify: function (mobile) {//手机号验证
  160. let rule = /^1\d{10}$/;
  161. return rule.test(mobile);
  162. },
  163. callMobile: function (e) {//拨打电话
  164. wx.makePhoneCall({
  165. phoneNumber: e //仅为示例,并非真实的电话号码
  166. })
  167. },
  168. sharePack: function () {//分享
  169. return {
  170. title: '',
  171. imageUrl: "",
  172. path: "/pages/index/index"
  173. }
  174. },
  175. addformId: function (e) {//添加formid
  176. this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
  177. console.log(res.msg);
  178. }, this, "POST");
  179. },
  180. getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
  181. wx.checkSession({
  182. success: res => {
  183. this.getMobile2(encryptedData,iv,callback,thisArg);
  184. },
  185. fail: res => {
  186. this.globalData.mobileData = {};
  187. this.globalData.mobileData.encryptedData = encryptedData;
  188. this.globalData.mobileData.iv = iv;
  189. this.globalData.mobileData.callback = callback;
  190. this.globalData.mobileData.thisArg = thisArg;
  191. this.wxLogin();
  192. }
  193. })
  194. },
  195. getMobile2: function (encryptedData, iv, callback, thisArg) {//和后台置换手机号
  196. this.globalData.mobileData = null;
  197. this.wxRequest(this.globalData.urlRoot + "userInfo/getUserPhoneNumber", { encryptedData: encryptedData, iv: iv }, res => {
  198. if (res.code == 200) {
  199. if (res.data && res.data.decodeData) {
  200. wx.setStorageSync('userMobile', res.data.decodeData.phoneNumber);
  201. this.globalData.userMobile = res.data.decodeData.phoneNumber;
  202. }
  203. }
  204. if (callback && thisArg) {
  205. callback.call(thisArg, res);
  206. }
  207. }, this, "POST");
  208. }
  209. })