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

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