东风启辰小程序端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

254 lines
7.8KB

  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. } else {
  94. wx.showToast({
  95. title: res.msg,
  96. icon: "none"
  97. })
  98. }
  99. }, this);
  100. },
  101. /**
  102. * wx.request
  103. */
  104. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  105. let that = this;
  106. var httpUrl = url;
  107. var str = "";
  108. var count = 0;
  109. for (let key in params) {
  110. if (count) {
  111. str += "&" + key + "=" + params[key];
  112. } else {
  113. str += key + "=" + params[key];
  114. }
  115. count++;
  116. }
  117. if (str) {
  118. httpUrl += "?" + str;
  119. }
  120. if (!methods) {
  121. methods = "GET";
  122. }
  123. if (methods == "POST") {
  124. wx.request({
  125. url: url,
  126. data: params,
  127. method: methods,
  128. header: {
  129. "content-type": "application/x-www-form-urlencoded",
  130. "OPENID": this.globalData.openid
  131. },
  132. success: function (re) {
  133. // if (re.data.code == -1002) {
  134. // wx.removeStorageSync('openid');
  135. // that.wxLogin();
  136. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  137. // }
  138. if (callback && thisArg) {
  139. callback.call(thisArg, re.data);
  140. }
  141. },
  142. fail: function (re) {
  143. wx.hideLoading();
  144. }
  145. })
  146. } else {
  147. wx.request({
  148. url: httpUrl,
  149. method: methods,
  150. header: {
  151. "OPENID": this.globalData.openid,
  152. },
  153. success: function (re) {
  154. if (re.data.code == -1002) {
  155. wx.removeStorageSync('openid');
  156. that.wxLogin();
  157. // that.wxRequest(url, params, callback, thisArg, methods, openid);
  158. }
  159. if (callback && thisArg) {
  160. callback.call(thisArg, re.data);
  161. }
  162. },
  163. fail: function (res) {
  164. wx.hideLoading();
  165. }
  166. })
  167. }
  168. },
  169. submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
  170. this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => {
  171. if (res.code == 200) {
  172. this.globalData.userInfoData = {};
  173. this.globalData.userInfoData.avatarUrl = avatarUrl;
  174. this.globalData.userInfoData.nickName = nickName;
  175. wx.setStorageSync('userInfoData', {
  176. avatarUrl: avatarUrl,
  177. nickName: nickName
  178. })
  179. }
  180. }, this, "POST")
  181. },
  182. codeVerify: function (card) {//身份证号码验证
  183. 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]$)/;
  184. return rule.test(card);
  185. },
  186. mobileVerify: function (mobile) {//手机号验证
  187. let rule = /^1\d{10}$/;
  188. return rule.test(mobile);
  189. },
  190. callMobile: function (e) {//拨打电话
  191. wx.makePhoneCall({
  192. phoneNumber: e //仅为示例,并非真实的电话号码
  193. })
  194. },
  195. sharePack: function () {//分享
  196. return {
  197. title: '',
  198. imageUrl: "",
  199. path: "/pages/index/index"
  200. }
  201. },
  202. addformId: function (e) {//添加formid
  203. this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
  204. console.log(res.msg);
  205. }, this, "POST");
  206. },
  207. getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
  208. console.log(encryptedData);
  209. console.log(iv);
  210. wx.checkSession({
  211. success: res => {
  212. this.getMobile2(encryptedData,iv,callback,thisArg);
  213. },
  214. fail: res => {
  215. this.globalData.mobileData = {};
  216. this.globalData.mobileData.encryptedData = encryptedData;
  217. this.globalData.mobileData.iv = iv;
  218. this.globalData.mobileData.callback = callback;
  219. this.globalData.mobileData.thisArg = thisArg;
  220. this.wxLogin();
  221. }
  222. })
  223. },
  224. getMobile2: function (encryptedData, iv, callback, thisArg) {//和后台置换手机号
  225. this.globalData.mobileData = null;
  226. this.wxRequest(this.globalData.urlRoot + "userInfo/getUserPhoneNumber", { encryptedData: encryptedData, iv: iv }, res => {
  227. if (res.code == 200) {
  228. if (res.data && res.data.decodeData) {
  229. wx.setStorageSync('userMobile', res.data.decodeData.phoneNumber);
  230. this.globalData.userMobile = res.data.decodeData.phoneNumber;
  231. }
  232. }
  233. if (callback && thisArg) {
  234. callback.call(thisArg, res);
  235. }
  236. }, this, "POST");
  237. },
  238. getUserInfo: function () {//获取个人信息
  239. this.wxRequest(this.globalData.urlRoot +"userInfo/getUserInfo",{},res=>{
  240. if(res.code==200){
  241. if (res.data && res.data.avatarUrl) {
  242. this.globalData.userInfoData = {};
  243. this.globalData.userInfoData.avatarUrl = res.data.avatarUrl;
  244. this.globalData.userInfoData.nickName = res.data.nickName;
  245. wx.setStorageSync('userInfoData', {
  246. avatarUrl: res.data.avatarUrl,
  247. nickName: res.data.nickName
  248. })
  249. }
  250. }
  251. },this)
  252. }
  253. })