东风启辰小程序端
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.

293 lines
9.1KB

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