东风启辰小程序端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

291 lines
9.0KB

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