东风启辰小程序端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

262 lines
8.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. }
  12. var userMobile = wx.getStorageSync('userMobile');
  13. if (userMobile){
  14. this.globalData.userMobile = userMobile;
  15. }
  16. var friendOpenid = wx.getStorageSync('friendOpenid');
  17. if (friendOpenid) {
  18. this.globalData.friendOpenid = friendOpenid;
  19. }
  20. var userInfoData = wx.getStorageSync("userInfoData");
  21. if (userInfoData){
  22. this.globalData.userInfoData = {};
  23. this.globalData.userInfoData.avatarUrl = userInfoData.avatarUrl;
  24. this.globalData.userInfoData.nickName = userInfoData.nickName;
  25. this.globalData.userInfoData.userLevel = userInfoData.userLevel;
  26. }else{
  27. if (openid){
  28. this.getUserInfo();
  29. }
  30. }
  31. var userPhoneType = wx.getStorageSync('userPhoneType');
  32. if (userPhoneType) {
  33. this.globalData.userPhoneType = userPhoneType;
  34. } else {
  35. this.getPhoneType();
  36. }
  37. },
  38. getPhoneType() {//获取手机类型
  39. wx.getSystemInfo({
  40. success: res => {
  41. wx.setStorageSync("userPhoneType", res.platform);
  42. this.globalData.userPhoneType = res.platform;
  43. }
  44. })
  45. },
  46. wxLogin() {
  47. wx.login({
  48. success: res => {
  49. if (res.code) {
  50. this.getOpenid(res.code);
  51. } else {
  52. console.log('登录失败!' + res.errMsg)
  53. }
  54. }
  55. })
  56. },
  57. globalData: {
  58. urlRoot: "https://dongfengqichen.jiyou-tech.com/",//测试接口根目录
  59. // urlRoot: "",//接口根目录
  60. urlStatic:"https://www.jiyou-tech.com/2020/496_qichen/static",//静态资源根目录
  61. openid: "",//OPENID
  62. friendOpenid:"",//推荐人的openid
  63. session_key: "",//session_key
  64. openidSuccessFuc: null,//方法回调
  65. nowPage:'1',//当前tabBar
  66. isRegister:false,//是否已注册
  67. userMobile:null,//用户手机号
  68. isFirstLogin:true,//是否为第一次登录
  69. indexData:{},//首页数据
  70. myCenterData:null,//个人中心数据
  71. userInfoData:null,
  72. certificationState:0,//1车主,2合伙人,3同事
  73. mobileData:null,
  74. userPhoneType:null,
  75. },
  76. // 获取openId
  77. getOpenid: function (code) {
  78. wx.showLoading({
  79. title: '加载中',
  80. mask: true,
  81. })
  82. this.wxRequest(this.globalData.urlRoot + "wxInfo/getOpenid", { code: code }, res => {
  83. wx.hideLoading();
  84. if (res.code == 200) {
  85. wx.setStorageSync("openid", res.data.openid);
  86. wx.setStorageSync("session_key", res.data.session_key);
  87. this.globalData.openid = res.data.openid;
  88. this.globalData.session_key = res.data.session_key;
  89. if (this.globalData.openidSuccessFuc) {
  90. this.globalData.openidSuccessFuc();
  91. }
  92. if(this.globalData.mobileData){
  93. this.getMobile2(this.globalData.mobileData.encryptedData, this.globalData.mobileData.iv, this.globalData.mobileData.callback, this.globalData.mobileData.thisArg);
  94. }
  95. var userInfoData = wx.getStorageSync("userInfoData");
  96. if (!userInfoData) {
  97. this.getUserInfo();
  98. }
  99. } else {
  100. wx.showToast({
  101. title: res.msg,
  102. icon: "none"
  103. })
  104. }
  105. }, this);
  106. },
  107. /**
  108. * wx.request
  109. */
  110. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  111. let that = this;
  112. var httpUrl = url;
  113. var str = "";
  114. var count = 0;
  115. for (let key in params) {
  116. if (count) {
  117. str += "&" + key + "=" + params[key];
  118. } else {
  119. str += key + "=" + params[key];
  120. }
  121. count++;
  122. }
  123. if (str) {
  124. httpUrl += "?" + str;
  125. }
  126. if (!methods) {
  127. methods = "GET";
  128. }
  129. if (methods == "POST") {
  130. wx.request({
  131. url: url,
  132. data: params,
  133. method: methods,
  134. header: {
  135. "content-type": "application/x-www-form-urlencoded",
  136. "OPENID": this.globalData.openid,
  137. "VERSION":100
  138. },
  139. success: function (re) {
  140. // if (re.data.code == -1002) {
  141. // wx.removeStorageSync('openid');
  142. // that.wxLogin();
  143. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  144. // }
  145. if (callback && thisArg) {
  146. callback.call(thisArg, re.data);
  147. }
  148. },
  149. fail: function (re) {
  150. wx.hideLoading();
  151. }
  152. })
  153. } else {
  154. wx.request({
  155. url: httpUrl,
  156. method: methods,
  157. header: {
  158. "OPENID": this.globalData.openid,
  159. },
  160. success: function (re) {
  161. if (re.data.code == -1002) {
  162. wx.removeStorageSync('openid');
  163. // that.wxLogin();
  164. // that.wxRequest(url, params, callback, thisArg, methods, openid);
  165. }
  166. if (callback && thisArg) {
  167. callback.call(thisArg, re.data);
  168. }
  169. },
  170. fail: function (res) {
  171. wx.hideLoading();
  172. }
  173. })
  174. }
  175. },
  176. submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
  177. this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => {
  178. if (res.code == 200) {
  179. this.globalData.userInfoData = {};
  180. this.globalData.userInfoData.avatarUrl = avatarUrl;
  181. this.globalData.userInfoData.nickName = nickName;
  182. wx.setStorageSync('userInfoData', {
  183. avatarUrl: avatarUrl,
  184. nickName: nickName,
  185. userLevel:1
  186. })
  187. }
  188. }, this, "POST")
  189. },
  190. codeVerify: function (card) {//身份证号码验证
  191. 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]$)/;
  192. return rule.test(card);
  193. },
  194. mobileVerify: function (mobile) {//手机号验证
  195. let rule = /^1\d{10}$/;
  196. return rule.test(mobile);
  197. },
  198. callMobile: function (e) {//拨打电话
  199. wx.makePhoneCall({
  200. phoneNumber: e //仅为示例,并非真实的电话号码
  201. })
  202. },
  203. sharePack: function () {//分享
  204. return {
  205. title: '',
  206. imageUrl: "",
  207. path: "/pages/index/index"
  208. }
  209. },
  210. addformId: function (e) {//添加formid
  211. this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
  212. console.log(res.msg);
  213. }, this, "POST");
  214. },
  215. getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
  216. wx.checkSession({
  217. success: res => {
  218. this.getMobile2(encryptedData,iv,callback,thisArg);
  219. },
  220. fail: res => {
  221. this.globalData.mobileData = {};
  222. this.globalData.mobileData.encryptedData = encryptedData;
  223. this.globalData.mobileData.iv = iv;
  224. this.globalData.mobileData.callback = callback;
  225. this.globalData.mobileData.thisArg = thisArg;
  226. this.wxLogin();
  227. }
  228. })
  229. },
  230. getMobile2: function (encryptedData, iv, callback, thisArg) {//和后台置换手机号
  231. this.globalData.mobileData = null;
  232. this.wxRequest(this.globalData.urlRoot + "userInfo/getUserPhoneNumber", { encryptedData: encryptedData, iv: iv }, res => {
  233. if (res.code == 200) {
  234. if (res.data && res.data.decodeData) {
  235. wx.setStorageSync('userMobile', res.data.decodeData.phoneNumber);
  236. this.globalData.userMobile = res.data.decodeData.phoneNumber;
  237. }
  238. }
  239. if (callback && thisArg) {
  240. callback.call(thisArg, res);
  241. }
  242. }, this, "POST");
  243. },
  244. getUserInfo: function () {//获取个人信息
  245. this.wxRequest(this.globalData.urlRoot +"userInfo/getUserInfo",{},res=>{
  246. if(res.code==200){
  247. if (res.data && res.data.avatarUrl) {
  248. this.globalData.userInfoData = {};
  249. this.globalData.userInfoData.avatarUrl = res.data.avatarUrl;
  250. this.globalData.userInfoData.nickName = res.data.nickName;
  251. this.globalData.userInfoData.userLevel = res.data.userLevel;
  252. wx.setStorageSync('userInfoData', {
  253. avatarUrl: res.data.avatarUrl,
  254. nickName: res.data.nickName,
  255. userLevel: res.data.userLevel
  256. })
  257. }
  258. }
  259. },this)
  260. }
  261. })