东风启辰小程序端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

335 lines
11KB

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