东风启辰小程序端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

225 rindas
6.9KB

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