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

264 lines
8.2KB

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