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

295 lines
9.3KB

  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. isFirstLucky: true,//是否为第一次进入幸运星抓手
  81. indexData:{},//首页数据
  82. myCenterData:null,//个人中心数据
  83. userInfoData:null,
  84. certificationState:0,//1车主,2合伙人,3同事
  85. mobileData:null,
  86. userPhoneType:null,
  87. kvurl:null,
  88. kvurlH:0,
  89. certificationInfo:false,
  90. getBuyState:null,
  91. entered:false,
  92. authenticationStatus:null,
  93. skipType:null
  94. },
  95. // 获取openId
  96. getOpenid: function (code) {
  97. wx.showLoading({
  98. title: '加载中',
  99. mask: true,
  100. })
  101. this.wxRequest(this.globalData.urlRoot + "wxInfo/getOpenid", { code: code }, res => {
  102. wx.hideLoading();
  103. if (res.code == 200) {
  104. wx.setStorageSync("openid", res.data.openid);
  105. wx.setStorageSync("session_key", res.data.session_key);
  106. this.globalData.openid = res.data.openid;
  107. this.globalData.session_key = res.data.session_key;
  108. if (this.globalData.openidSuccessFuc) {
  109. this.globalData.openidSuccessFuc();
  110. }
  111. if(this.globalData.mobileData){
  112. this.getMobile2(this.globalData.mobileData.encryptedData, this.globalData.mobileData.iv, this.globalData.mobileData.callback, this.globalData.mobileData.thisArg);
  113. }
  114. var userInfoData = wx.getStorageSync("userInfoData");
  115. if (!userInfoData) {
  116. this.getUserInfo();
  117. }
  118. this.getBuyState();
  119. } else {
  120. wx.showToast({
  121. title: res.msg,
  122. icon: "none"
  123. })
  124. }
  125. }, this);
  126. },
  127. /**
  128. * wx.request
  129. */
  130. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  131. let that = this;
  132. var httpUrl = url;
  133. var str = "";
  134. var count = 0;
  135. for (let key in params) {
  136. if (count) {
  137. str += "&" + key + "=" + params[key];
  138. } else {
  139. str += key + "=" + params[key];
  140. }
  141. count++;
  142. }
  143. if (str) {
  144. httpUrl += "?" + str;
  145. }
  146. if (!methods) {
  147. methods = "GET";
  148. }
  149. if (methods == "POST") {
  150. wx.request({
  151. url: url,
  152. data: params,
  153. method: methods,
  154. header: {
  155. "content-type": "application/x-www-form-urlencoded",
  156. "VERSION": '100',
  157. "OPENID": this.globalData.openid
  158. },
  159. success: function (re) {
  160. // if (re.data.code == -1002) {
  161. // wx.removeStorageSync('openid');
  162. // that.wxLogin();
  163. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  164. // }
  165. if (callback && thisArg) {
  166. callback.call(thisArg, re.data);
  167. }
  168. },
  169. fail: function (re) {
  170. wx.hideLoading();
  171. }
  172. })
  173. } else {
  174. wx.request({
  175. url: httpUrl,
  176. method: methods,
  177. header: {
  178. "OPENID": this.globalData.openid,
  179. "VERSION": '100'
  180. },
  181. success: function (re) {
  182. if (re.data.code == -1002) {
  183. wx.removeStorageSync('openid');
  184. that.wxLogin();
  185. // that.wxRequest(url, params, callback, thisArg, methods, openid);
  186. }
  187. if (callback && thisArg) {
  188. callback.call(thisArg, re.data);
  189. }
  190. },
  191. fail: function (res) {
  192. wx.hideLoading();
  193. }
  194. })
  195. }
  196. },
  197. submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
  198. this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => {
  199. if (res.code == 200) {
  200. this.globalData.userInfoData = {};
  201. this.globalData.userInfoData.avatarUrl = avatarUrl;
  202. this.globalData.userInfoData.nickName = nickName;
  203. wx.setStorageSync('userInfoData', {
  204. avatarUrl: avatarUrl,
  205. nickName: nickName,
  206. userLevel:1
  207. })
  208. }
  209. }, this, "POST")
  210. },
  211. codeVerify: function (card) {//身份证号码验证
  212. 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]$)/;
  213. return rule.test(card);
  214. },
  215. mobileVerify: function (mobile) {//手机号验证
  216. let rule = /^1\d{10}$/;
  217. return rule.test(mobile);
  218. },
  219. callMobile: function (e) {//拨打电话
  220. wx.makePhoneCall({
  221. phoneNumber: e //仅为示例,并非真实的电话号码
  222. })
  223. },
  224. sharePack: function () {//分享
  225. return {
  226. title: '',
  227. imageUrl: "",
  228. path: "/pages/index/index"
  229. }
  230. },
  231. addformId: function (e) {//添加formid
  232. this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
  233. console.log(res.msg);
  234. }, this, "POST");
  235. },
  236. getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
  237. wx.checkSession({
  238. success: res => {
  239. this.getMobile2(encryptedData,iv,callback,thisArg);
  240. },
  241. fail: res => {
  242. this.globalData.mobileData = {};
  243. this.globalData.mobileData.encryptedData = encryptedData;
  244. this.globalData.mobileData.iv = iv;
  245. this.globalData.mobileData.callback = callback;
  246. this.globalData.mobileData.thisArg = thisArg;
  247. this.wxLogin();
  248. }
  249. })
  250. },
  251. getMobile2: function (encryptedData, iv, callback, thisArg) {//和后台置换手机号
  252. this.globalData.mobileData = null;
  253. this.wxRequest(this.globalData.urlRoot + "userInfo/getUserPhoneNumber", { encryptedData: encryptedData, iv: iv }, res => {
  254. if (res.code == 200) {
  255. if (res.data && res.data.decodeData) {
  256. wx.setStorageSync('userMobile', res.data.decodeData.phoneNumber);
  257. this.globalData.userMobile = res.data.decodeData.phoneNumber;
  258. }
  259. }
  260. if (callback && thisArg) {
  261. callback.call(thisArg, res);
  262. }
  263. }, this, "POST");
  264. },
  265. getUserInfo: function () {//获取个人信息
  266. this.wxRequest(this.globalData.urlRoot +"userInfo/getUserInfo",{},res=>{
  267. if(res.code==200){
  268. if (res.data && res.data.avatarUrl) {
  269. this.globalData.userInfoData = {};
  270. this.globalData.userInfoData.avatarUrl = res.data.avatarUrl;
  271. this.globalData.userInfoData.nickName = res.data.nickName;
  272. this.globalData.userInfoData.userLevel = res.data.userLevel;
  273. wx.setStorageSync('userInfoData', {
  274. avatarUrl: res.data.avatarUrl,
  275. nickName: res.data.nickName,
  276. userLevel: res.data.userLevel
  277. })
  278. }
  279. }
  280. },this)
  281. },
  282. getBuyState: function () {//查询是否已下订
  283. this.wxRequest(this.globalData.urlRoot + "wxPay/getBuyState", {}, res => {
  284. if (res.code == 200) {
  285. if (res.data) {
  286. res.data.cdate = res.data.cdate ? res.data.cdate : "";
  287. this.globalData.getBuyState = res.data;
  288. if (this.globalData.buyStateSuccessFuc){
  289. this.globalData.buyStateSuccessFuc();
  290. }
  291. }
  292. }
  293. }, this);
  294. }
  295. })