东风启辰小程序端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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