东风启辰小程序端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

294 lines
9.2KB

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