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

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