东风启辰小程序端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

269 linhas
8.4KB

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