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

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