东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
9.8KB

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