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

328 lines
11KB

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