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

260 satır
8.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. }
  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. }else{
  26. if (openid){
  27. this.getUserInfo();
  28. }
  29. }
  30. var userPhoneType = wx.getStorageSync('userPhoneType');
  31. if (userPhoneType) {
  32. this.globalData.userPhoneType = userPhoneType;
  33. } else {
  34. this.getPhoneType();
  35. }
  36. },
  37. getPhoneType() {//获取手机类型
  38. wx.getSystemInfo({
  39. success: res => {
  40. wx.setStorageSync("userPhoneType", res.platform);
  41. this.globalData.userPhoneType = res.platform;
  42. }
  43. })
  44. },
  45. wxLogin() {
  46. wx.login({
  47. success: res => {
  48. if (res.code) {
  49. this.getOpenid(res.code);
  50. } else {
  51. console.log('登录失败!' + res.errMsg)
  52. }
  53. }
  54. })
  55. },
  56. globalData: {
  57. urlRoot: "https://dongfengqichen.jiyou-tech.com/",//测试接口根目录
  58. // urlRoot: "",//接口根目录
  59. urlStatic:"https://www.jiyou-tech.com/2020/496_qichen/static",//静态资源根目录
  60. openid: "",//OPENID
  61. friendOpenid:"",//推荐人的openid
  62. session_key: "",//session_key
  63. openidSuccessFuc: null,//方法回调
  64. nowPage:'1',//当前tabBar
  65. isRegister:false,//是否已注册
  66. userMobile:null,//用户手机号
  67. isFirstLogin:true,//是否为第一次登录
  68. indexData:{},//首页数据
  69. myCenterData:null,//个人中心数据
  70. userInfoData:null,
  71. certificationState:0,//1车主,2合伙人,3同事
  72. mobileData:null,
  73. userPhoneType:null,
  74. },
  75. // 获取openId
  76. getOpenid: function (code) {
  77. wx.showLoading({
  78. title: '加载中',
  79. mask: true,
  80. })
  81. this.wxRequest(this.globalData.urlRoot + "wxInfo/getOpenid", { code: code }, res => {
  82. wx.hideLoading();
  83. console.log(res);
  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. },
  138. success: function (re) {
  139. // if (re.data.code == -1002) {
  140. // wx.removeStorageSync('openid');
  141. // that.wxLogin();
  142. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  143. // }
  144. if (callback && thisArg) {
  145. callback.call(thisArg, re.data);
  146. }
  147. },
  148. fail: function (re) {
  149. wx.hideLoading();
  150. }
  151. })
  152. } else {
  153. wx.request({
  154. url: httpUrl,
  155. method: methods,
  156. header: {
  157. "OPENID": this.globalData.openid,
  158. },
  159. success: function (re) {
  160. if (re.data.code == -1002) {
  161. wx.removeStorageSync('openid');
  162. that.wxLogin();
  163. // that.wxRequest(url, params, callback, thisArg, methods, openid);
  164. }
  165. if (callback && thisArg) {
  166. callback.call(thisArg, re.data);
  167. }
  168. },
  169. fail: function (res) {
  170. wx.hideLoading();
  171. }
  172. })
  173. }
  174. },
  175. submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
  176. this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => {
  177. if (res.code == 200) {
  178. this.globalData.userInfoData = {};
  179. this.globalData.userInfoData.avatarUrl = avatarUrl;
  180. this.globalData.userInfoData.nickName = nickName;
  181. wx.setStorageSync('userInfoData', {
  182. avatarUrl: avatarUrl,
  183. nickName: nickName
  184. })
  185. }
  186. }, this, "POST")
  187. },
  188. codeVerify: function (card) {//身份证号码验证
  189. 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]$)/;
  190. return rule.test(card);
  191. },
  192. mobileVerify: function (mobile) {//手机号验证
  193. let rule = /^1\d{10}$/;
  194. return rule.test(mobile);
  195. },
  196. callMobile: function (e) {//拨打电话
  197. wx.makePhoneCall({
  198. phoneNumber: e //仅为示例,并非真实的电话号码
  199. })
  200. },
  201. sharePack: function () {//分享
  202. return {
  203. title: '',
  204. imageUrl: "",
  205. path: "/pages/index/index"
  206. }
  207. },
  208. addformId: function (e) {//添加formid
  209. this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
  210. console.log(res.msg);
  211. }, this, "POST");
  212. },
  213. getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
  214. console.log(encryptedData);
  215. console.log(iv);
  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. wx.setStorageSync('userInfoData', {
  252. avatarUrl: res.data.avatarUrl,
  253. nickName: res.data.nickName
  254. })
  255. }
  256. }
  257. },this)
  258. }
  259. })