东风启辰小程序端
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.

193 lines
5.7KB

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