request.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. const app = getApp();
  2. let WebIM = (wx.WebIM = require("../hxChatSDK/utils/WebIM")["default"]);
  3. const env = {
  4. NODE_ENV: 'pota',
  5. dev: {
  6. apiUrl: 'https://apitest.zhiqiyun.com'
  7. },
  8. prd: {
  9. apiUrl: 'https://apimall.zhiqiyun.com'
  10. },
  11. pota: {
  12. // apiUrl: 'http://192.168.110.98:8098'
  13. // apiUrl: 'http://192.168.110.180:8098' ,//f
  14. // apiUrl: 'http://192.168.110.182:8098' ,//z
  15. apiUrl: 'http://192.168.110.176:8098' //y
  16. // apiUrl: 'http://192.168.110.117:8098'//l
  17. // apiUrl: 'http://192.168.110.216:8098'
  18. // apiUrl: 'http://192.168.110.180:8098' //wjg
  19. }
  20. }
  21. var header = {
  22. 'content-type': 'application/x-www-form-urlencoded',
  23. 'appId': 'ZQhe229Ax49Pdpy', //中国人寿测试环境
  24. }
  25. /**小程序配置 */
  26. const publics = {
  27. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  28. 'copyrighttitle': '智企云', //底部版权公司名称
  29. 'copyrighttel': '400-698-5980', //底部版权服务电话
  30. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  31. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  32. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  33. 'channel': '', //处方中间渠道号 SAAS系统为空
  34. 'is_customer_pick_up': false,
  35. // 'adGroupId': '1196269897935630352', //享团-宜得了
  36. 'mapLBSKEY': 'YQZBZ-6LMC4-IGQUO-XE64O-4UJL6-YNB77',
  37. 'iconSrc': 'red/',
  38. 'titleTopBgColor': '#FFF',
  39. // 小程序ID wxe7bec198ecfe1162
  40. }
  41. /*
  42. * 获取token
  43. */
  44. const getToken = (success) => {
  45. let token = getStorage('AUTH_TOKEN');
  46. if (token) {
  47. success.call(this, token);
  48. return false;
  49. } else {
  50. loadToken(data => {
  51. success.call(this, data);
  52. });
  53. }
  54. }
  55. const loadToken = (success) => {
  56. let that = this
  57. uni.request({
  58. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  59. data: {},
  60. method: 'get',
  61. header: header,
  62. success(json) {
  63. if (json.statusCode !== 200) return msg(json.data.message);
  64. if (json.data.code !== 0) return msg(json.data.msg)
  65. setStorage('AUTH_TOKEN', json.data.data);
  66. success.call(that, json.data.data);
  67. },
  68. fail() {
  69. msg('access_token 获取失败');
  70. }
  71. })
  72. }
  73. /**
  74. * 微信小程序静默授权登录
  75. * @param {*} parentId
  76. * @param {*} merchantId
  77. */
  78. const silenceLogin = (parentId, merchantId) => {
  79. // #ifdef MP-WEIXIN
  80. if (!isLogins(false)) { //未登录
  81. uni.login({
  82. success(res) {
  83. console.log('静默授权登录>>>', res);
  84. let params = {
  85. code: res.code,
  86. parentId: parentId ? parentId : '',
  87. merchantId: merchantId ? merchantId : ''
  88. }
  89. return new Promise((resolve, reject) => {
  90. postRequest('/api/v3/silence/login', params, data => {
  91. if (data.token) setStorage('AUTH_TOKEN', data.token);
  92. getRequest('/api/user/info', {}, suc => {
  93. data.isDistriUser = suc.isDistriUser;
  94. setStorage('userInfo', data);
  95. getIMToken(false);
  96. });
  97. resolve();
  98. });
  99. })
  100. }
  101. });
  102. }
  103. // #endif
  104. }
  105. const getIMToken = (isUpdatToken) => {
  106. var user = getStorage('userInfo');
  107. getRequest('/api/im/msg/userToken', {}, suc => {
  108. setStorage('imToken', suc);
  109. if (isUpdatToken) {
  110. //更新token
  111. uni.setStorage({
  112. key: "myUsername",
  113. data: 'RS_' + user.id
  114. });
  115. getApp().globalData.conn.open({
  116. user: 'RS_' + user.id,
  117. accessToken: getStorage('imToken'),
  118. });
  119. getApp().globalData.onLoginSuccess(uni.getStorageSync("myUsername"));
  120. } else {
  121. //初始化IM
  122. uni.setStorage({
  123. key: "myUsername",
  124. data: 'RS_' + user.id
  125. });
  126. getApp().globalData.conn.open({
  127. apiUrl: WebIM.config.apiURL,
  128. user: 'RS_' + user.id,
  129. pwd: getStorage('imToken'),
  130. accessToken: getStorage('imToken'),
  131. //grant_type: this.data.grant_type,
  132. appKey: WebIM.config.appkey
  133. });
  134. }
  135. }, true);
  136. }
  137. /**
  138. * 跳转进入消息列表
  139. */
  140. const jumpConversation = () => {
  141. if (!isLogins(true)) { //未登录
  142. return;
  143. }
  144. getIMToken(true);
  145. }
  146. const getWXUserProfile = (suc) => {
  147. let that = this;
  148. var sessionKey = '';
  149. var openid = '';
  150. getApp().globalData.getCheckSessoin(json => {
  151. sessionKey = json.session_key;
  152. openid = json.openid;
  153. });
  154. try {
  155. uni.getUserProfile({
  156. desc: '完善信息',
  157. success: res => {
  158. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  159. },
  160. fail: res => {
  161. console.log('授权失败数据>>>', res);
  162. msg(res);
  163. }
  164. });
  165. } catch (e) {
  166. // 老版
  167. wx.getUserInfo({
  168. success: function(ress) {
  169. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  170. }
  171. });
  172. }
  173. }
  174. /**
  175. * 保存微信头像
  176. */
  177. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  178. if (!encryptedData || !iv) return false;
  179. let _params = {
  180. sessionKey: sessionKey,
  181. openId: openid,
  182. iv: iv,
  183. encryptedData: encryptedData
  184. };
  185. postRequest('/api/user/weixin/update', _params, json => {
  186. setStorage('userInfo', json);
  187. suc.call();
  188. });
  189. }
  190. const clearValueEmpty = (data) => {
  191. let keyValue = {};
  192. for (let key in data) {
  193. let value = typeof data[key];
  194. if (value == 'string' && value) {
  195. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  196. keyValue[key] = data[key];
  197. }
  198. // keyValue[key] = data[key];
  199. } else if (value == 'number' && value != null) {
  200. keyValue[key] = data[key];
  201. } else if (value == 'boolean') {
  202. keyValue[key] = data[key];
  203. } else {
  204. if (data[key]) keyValue[key] = data[key];
  205. }
  206. }
  207. return keyValue;
  208. }
  209. /**
  210. * 公共请求
  211. *
  212. * @param {*} url 请求url
  213. * @param {*} data 请求参数
  214. * @param {*} method 请求方法
  215. * @param {*} success 成功函数
  216. * @param {*} isLoad 是否显示加载提示
  217. */
  218. const baseRequest = (url, data, method, success, isLoad) => {
  219. getToken(token => {
  220. if (!isLoad) uni.showNavigationBarLoading();
  221. header.authorityToken = token;
  222. // if (url.indexOf('/v3') != -1 || url.indexOf('/paper/create') != -1) {
  223. // header['content-type'] = 'application/json;charset=UTF-8'
  224. // } else {
  225. header['content-type'] = 'application/json;charset=UTF-8'
  226. // }
  227. uni.request({
  228. url: env[env.NODE_ENV].apiUrl + url,
  229. data: clearValueEmpty(data),
  230. method: method,
  231. header: header,
  232. success(json) {
  233. if (!isLoad) uni.hideNavigationBarLoading();
  234. if (json.statusCode !== 200) return msg(json.data.message);
  235. if (json.data.code === 10021 || json.data.code === 10020) {
  236. // console.log("第二次执行")
  237. removeStorage('AUTH_TOKEN');
  238. return redirectIndex();
  239. }
  240. if (json.data.code !== 0) {
  241. return msg(json.data.msg)
  242. }
  243. // console.log("第二次执行")
  244. let data = json.data.data;
  245. if (typeof data === 'string' && data.indexOf("{") === 0) {
  246. data = JSON.parse(data);
  247. }
  248. success.call(this, data);
  249. },
  250. fail() {
  251. if (!isLoad) uni.hideNavigationBarLoading();
  252. },
  253. // complete(){
  254. // success.call(this, data);
  255. // },
  256. })
  257. })
  258. }
  259. const loadIng = (msg) => {
  260. uni.showLoading({
  261. title: msg,
  262. mask: true
  263. })
  264. }
  265. /**POST请求 */
  266. const postRequest = (url, data, success, isLoad) => {
  267. header['content-type'] = 'application/json;charset=UTF-8'
  268. baseRequest(url, data, 'post', success, isLoad);
  269. }
  270. const postRequestLoding = (url, data, success, isLoding) => {
  271. header['content-type'] = 'application/json;charset=UTF-8'
  272. baseRequest(url, data, 'post', success, false, isLoding);
  273. }
  274. /**GET请求 */
  275. const getRequest = (url, data, success, isLoad) => {
  276. header['content-type'] = 'application/json;charset=UTF-8'
  277. baseRequest(url, data, 'get', success, isLoad);
  278. }
  279. /**Put请求 */
  280. const putRequest = (url, data, success, isLoad) => {
  281. header['content-type'] = 'application/json;charset=UTF-8'
  282. baseRequest(url, data, 'put', success, isLoad)
  283. }
  284. const putRequestJson = (url, data, success, isLoad) => {
  285. header['content-type'] = 'application/json;charset=UTF-8'
  286. baseRequest(url, data, 'put', success, isLoad)
  287. }
  288. /**
  289. * 上传文件
  290. * @param {*} url 请求url
  291. * @param {*} data 携带数据
  292. * @param {*} success 请求成功函数
  293. */
  294. const uploadFile = (url, data, success) => {
  295. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  296. getToken(token => {
  297. uni.uploadFile({
  298. url: env[env.NODE_ENV].apiUrl + url,
  299. filePath: data,
  300. name: 'file',
  301. header: {
  302. 'appId': header['appId'],
  303. 'authorityToken': token
  304. },
  305. success(res) {
  306. if (res.statusCode !== 200) return msg('文件上传失败');
  307. let data = JSON.parse(res.data);
  308. if (data.code !== 0) return msg(data.msg);
  309. success.call(this, data.data);
  310. },
  311. })
  312. })
  313. }
  314. /**弹窗 */
  315. const msg = (title, success) => {
  316. if (title) {
  317. uni.showToast({
  318. title: title,
  319. icon: 'none',
  320. success() {
  321. if (success) success.call(this);
  322. }
  323. });
  324. }
  325. return false;
  326. }
  327. const msgConfirm = (msg, success, cancel) => {
  328. uni.showModal({
  329. title: '提示',
  330. content: msg,
  331. success(res) {
  332. if (res.confirm) {
  333. success.call(this);
  334. } else {
  335. if (cancel) cancel.call(this);
  336. }
  337. }
  338. })
  339. }
  340. const msgConfirmText = (msg, confirmText, success, cancel) => {
  341. uni.showModal({
  342. title: '提示',
  343. content: msg,
  344. confirmText: confirmText,
  345. success(res) {
  346. if (res.confirm) {
  347. success.call(this);
  348. } else {
  349. if (cancel) cancel.call(this);
  350. }
  351. }
  352. })
  353. }
  354. const alertMsg = (msg, success) => {
  355. uni.showModal({
  356. title: '提示',
  357. content: msg,
  358. showCancel: false,
  359. success(res) {
  360. if (success) success.call(this);
  361. }
  362. })
  363. return false;
  364. }
  365. const load = msg => {
  366. uni.showLoading({
  367. title: msg,
  368. mask: true
  369. })
  370. }
  371. const g = (url, success, isLoad) => {
  372. getRequest(url, {}, success, isLoad)
  373. }
  374. const p = (url, success, isLoad) => {
  375. postRequest(url, {}, success, isLoad)
  376. }
  377. const send = (url, mobile, success, error) => {
  378. if (!mobile) {
  379. return msg('手机号码不能为空', error);
  380. }
  381. postRequest(url, {
  382. phone: mobile
  383. }, json => {
  384. if (json.code !== 0) {
  385. return msg(json.msg, error);
  386. }
  387. let time = 60;
  388. const initTime = setInterval(() => {
  389. time--;
  390. if (time > 0) {
  391. success.call(this, time + '秒后获取');
  392. } else {
  393. clearInterval(initTime);
  394. success.call(this, '获取验证码');
  395. }
  396. }, 1000)
  397. })
  398. }
  399. // 登陆拦截
  400. const redirectIndex = (params) => {
  401. // uni.clearStorageSync();
  402. // console.log(params)
  403. var pages = getCurrentPages();
  404. // console.log("pages》》》》》登陆拦截", pages);
  405. let currentUrl;
  406. if (pages) {
  407. var currentPage = pages[pages.length - 1];
  408. if (currentPage) {
  409. currentUrl = currentPage.route;
  410. let query = currentPage.data.query;
  411. if (query) {
  412. for (let key in query) {
  413. const fo = key + '=' + query[key];
  414. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  415. }
  416. }
  417. if (currentUrl) {
  418. console.log('currentUrl》》》》》', currentUrl);
  419. setStorage("REDIRECT_URL", '/' + currentUrl)
  420. if (currentUrl == 'pages/authorize/authorize') {
  421. console.log('当前已经处于登录页面,不在向下执行跳转');
  422. return
  423. }
  424. }
  425. }
  426. }
  427. let redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  428. // console.log("登陆拦截")
  429. uni.navigateTo({
  430. url: redirectUrl
  431. })
  432. return false;
  433. }
  434. const saveImage = (url) => {
  435. uni.saveImageToPhotosAlbum({
  436. filePath: url,
  437. success() {
  438. msg('图片保存成功');
  439. }
  440. })
  441. }
  442. const saveImageToPhotosAlbum = (url) => {
  443. if (!url) return msg('小程序码不存在');
  444. uni.getImageInfo({
  445. src: url,
  446. success(json) {
  447. // uni.getSetting({
  448. // success(res) {
  449. // if (!res.authSetting['scope.writePhotosAlbum']) {
  450. // uni.authorize({
  451. // scope: 'scope.writePhotosAlbum',
  452. // success() {
  453. // saveImage(json.path);
  454. // }
  455. // })
  456. // } else {
  457. // saveImage(json.path);
  458. // }
  459. // }
  460. // })
  461. }
  462. })
  463. }
  464. const getConfig = (config, id) => {
  465. let con = {};
  466. config.info.forEach(inf => {
  467. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  468. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  469. con[inf.setName] = inf.setDefault;
  470. });
  471. config.card.forEach(inf => {
  472. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  473. con[inf.setName] = inf.setDefault;
  474. });
  475. config.realTime.forEach(inf => {
  476. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  477. con[inf.setName] = inf.setDefault;
  478. });
  479. return con;
  480. }
  481. const isLogin = async (error) => {
  482. let userInfo = getStorage('userInfo');
  483. if (!userInfo || !userInfo.id) {
  484. if (error) return false;
  485. // console.log("第一次执行")
  486. return redirectIndex();
  487. }
  488. return true;
  489. }
  490. //检测是否登录
  491. const isLogins = (isGoLogin) => {
  492. let userInfo = getStorage('userInfo');
  493. if (!userInfo) {
  494. if (isGoLogin) {
  495. redirectIndex();
  496. }
  497. return false;
  498. }
  499. return true;
  500. }
  501. const setStorage = (key, value) => {
  502. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  503. }
  504. const getStorage = (key) => {
  505. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  506. }
  507. const removeStorage = (key) => {
  508. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  509. }
  510. /**
  511. * 缓存模板id
  512. */
  513. // const gettmplIds = () => {
  514. // getRequest('/api/wxmes/querylist', {}, res => {
  515. // setStorage('tmplIds', res)
  516. // console.log(res)
  517. // })
  518. // }
  519. //微信支付订单公共方法
  520. const payOrder = (id, success) => {
  521. var loctionAddressMap = getStorage('loctionAddressMap');
  522. var datas = {
  523. id: id
  524. };
  525. if (loctionAddressMap) {
  526. datas.province = loctionAddressMap.province;
  527. datas.city = loctionAddressMap.city;
  528. };
  529. if (getStorage('isVideoScene')) {
  530. uni.checkBeforeAddOrder({
  531. success(res) {
  532. console.log('下单前置检查', res)
  533. let data = res.data;
  534. data.requiredFundType = 0;
  535. datas.sceneCheckStr = JSON.stringify(data);
  536. wexinPay(datas, success)
  537. },
  538. })
  539. } else {
  540. wexinPay(datas, success)
  541. }
  542. }
  543. //支付中或已支付
  544. const payStatus = (json, id, success) => {
  545. if (json && json.code === 115) {
  546. setTimeout(res => {
  547. uni.hideLoading();
  548. isShowLoading = false;
  549. uni.showModal({
  550. title: '提示',
  551. content: '当前订单正在支付或30秒后重试',
  552. confirmText: '我知道了',
  553. showCancel: false,
  554. success(res) {}
  555. })
  556. }, 3000)
  557. return false;
  558. }
  559. if (json.type === 2 || json.type === 3) {
  560. //调用后台判断订单是否支付成功
  561. let i = 0;
  562. let timer = setInterval(() => {
  563. postRequest('/api/order/check', {
  564. id: id
  565. }, res => {
  566. if (res.isSuccess || i === 4) {
  567. clearInterval(timer);
  568. success.call(this, res.isSuccess);
  569. } else {
  570. i++;
  571. }
  572. }, true)
  573. }, 200);
  574. // success.call(this);
  575. return false;
  576. }
  577. }
  578. // 支付成功回调
  579. const paySuccess = (id, success) => {
  580. console.log('支付成功')
  581. let i = 0;
  582. let timer = setInterval(() => {
  583. postRequest('/api/order/check', {
  584. id: id
  585. }, res => {
  586. console.log(i)
  587. if (res.isSuccess || i === 5) {
  588. clearInterval(timer);
  589. success.call(this, res.isSuccess);
  590. } else {
  591. i++;
  592. }
  593. }, true)
  594. }, 500);
  595. }
  596. // 支付失败回调
  597. const payFail = (id) => {
  598. console.log('支付失败')
  599. postRequest('/api/order/cancelPay', {
  600. id: id
  601. }, data => {
  602. uni.redirectTo({
  603. url: '/mine/orderDet/orderDet?id=' + id,
  604. })
  605. })
  606. }
  607. const wexinPay = (datas, success) => {
  608. let isShowLoading = false;
  609. if (!isShowLoading) {
  610. loadIng('加载中');
  611. // console.log('加载中')
  612. isShowLoading = true;
  613. }
  614. // #ifdef MP-WEIXIN
  615. postRequest('/api/order/weixin/pay', datas, json => {
  616. // console.log('json',json)
  617. payStatus(json, datas.id, success)
  618. const sceneCheckStr = datas.sceneCheckStr;
  619. if (sceneCheckStr && JSON.parse(sceneCheckStr).requiredFundType === 1) {
  620. console.log('拉起收银台')
  621. wx.requestOrderPayment({
  622. timeStamp: json.timeStamp,
  623. nonceStr: json.nonceStr,
  624. package: json.packages,
  625. signType: json.signType,
  626. orderInfo: json.orderInfo,
  627. paySign: json.sign,
  628. success: function() {
  629. paySuccess(datas.id, success)
  630. },
  631. fail: function(res) {
  632. console.log(res);
  633. payFail(datas.id)
  634. }
  635. })
  636. } else {
  637. uni.requestPayment({
  638. timeStamp: json.timeStamp,
  639. nonceStr: json.nonceStr,
  640. package: json.packages,
  641. signType: json.signType,
  642. paySign: json.sign,
  643. success: function() {
  644. paySuccess(datas.id, success)
  645. },
  646. fail: function(res) {
  647. console.log(res);
  648. payFail(datas.id)
  649. }
  650. })
  651. }
  652. if (isShowLoading) {
  653. uni.hideLoading();
  654. isShowLoading = false;
  655. }
  656. })
  657. // #endif
  658. // #ifdef MP-ALIPAY
  659. datas.payChannel = 'alipay_lite ';
  660. postRequest('/api/order/weixin/pay', datas, json => {
  661. // console.log(json)
  662. if (json.type === 2 || json.type === 3) {
  663. //调用后台判断订单是否支付成功
  664. let i = 0;
  665. let timer = setInterval(() => {
  666. postRequest('/api/order/check', {
  667. id: id
  668. }, res => {
  669. if (res.isSuccess || i === 4) {
  670. clearInterval(timer);
  671. success.call(this, res.isSuccess);
  672. } else {
  673. i++;
  674. }
  675. }, true)
  676. }, 200);
  677. // success.call(this);
  678. return false;
  679. }
  680. uni.requestPayment({
  681. provider: 'alipay',
  682. orderInfo: json.packages,
  683. success: function() {
  684. paySuccess(datas.id, success)
  685. },
  686. fail: function(res) {
  687. payFail(datas.id)
  688. console.log(res);
  689. }
  690. })
  691. if (isShowLoading) {
  692. uni.hideLoading();
  693. isShowLoading = false;
  694. }
  695. })
  696. // #endif
  697. }
  698. //微信支付订单公共方法
  699. const payOrdersss = (id, success) => {
  700. let isShowLoading = false;
  701. if (!isShowLoading) {
  702. loadIng('加载中');
  703. // console.log('加载中')
  704. isShowLoading = true;
  705. }
  706. var loctionAddressMap = getStorage('loctionAddressMap');
  707. var datas = {
  708. id: id
  709. };
  710. if (loctionAddressMap) {
  711. datas.province = loctionAddressMap.province;
  712. datas.city = loctionAddressMap.city;
  713. };
  714. // #ifdef MP-WEIXIN
  715. postRequest('/api/order/weixin/pay', datas, json => {
  716. // console.log(json)
  717. if (json.type === 2 || json.type === 3) {
  718. //调用后台判断订单是否支付成功
  719. let i = 0;
  720. let timer = setInterval(() => {
  721. postRequest('/api/order/check', {
  722. id: id
  723. }, res => {
  724. if (res.isSuccess || i === 4) {
  725. clearInterval(timer);
  726. success.call(this, res.isSuccess);
  727. } else {
  728. i++;
  729. }
  730. }, true)
  731. }, 200);
  732. // success.call(this);
  733. return false;
  734. }
  735. uni.requestPayment({
  736. timeStamp: json.timeStamp,
  737. nonceStr: json.nonceStr,
  738. package: json.packages,
  739. signType: json.signType,
  740. paySign: json.sign,
  741. success: function() {
  742. console.log('支付成功')
  743. let i = 0;
  744. let timer = setInterval(() => {
  745. postRequest('/api/order/check', {
  746. id: id
  747. }, res => {
  748. console.log(i)
  749. if (res.isSuccess || i === 5) {
  750. clearInterval(timer);
  751. success.call(this, res.isSuccess);
  752. } else {
  753. i++;
  754. }
  755. }, true)
  756. }, 500);
  757. },
  758. fail: function(res) {
  759. console.log('支付失败')
  760. postRequest('/api/order/cancelPay', {
  761. id: id
  762. }, data => {
  763. uni.redirectTo({
  764. url: '/mine/orderDet/orderDet?id=' + id,
  765. })
  766. })
  767. console.log(res);
  768. }
  769. })
  770. if (isShowLoading) {
  771. uni.hideLoading();
  772. isShowLoading = false;
  773. }
  774. })
  775. // #endif
  776. // #ifdef MP-ALIPAY
  777. datas.payChannel = 'alipay_lite ';
  778. postRequest('/api/order/weixin/pay', datas, json => {
  779. // console.log(json)
  780. if (json.type === 2 || json.type === 3) {
  781. //调用后台判断订单是否支付成功
  782. let i = 0;
  783. let timer = setInterval(() => {
  784. postRequest('/api/order/check', {
  785. id: id
  786. }, res => {
  787. if (res.isSuccess || i === 4) {
  788. clearInterval(timer);
  789. success.call(this, res.isSuccess);
  790. } else {
  791. i++;
  792. }
  793. }, true)
  794. }, 200);
  795. // success.call(this);
  796. return false;
  797. }
  798. uni.requestPayment({
  799. provider: 'alipay',
  800. orderInfo: json.packages,
  801. success: function() {
  802. console.log('支付成功')
  803. let i = 0;
  804. let timer = setInterval(() => {
  805. postRequest('/api/order/check', {
  806. id: id
  807. }, res => {
  808. console.log(i)
  809. if (res.isSuccess || i === 5) {
  810. clearInterval(timer);
  811. success.call(this, res.isSuccess);
  812. } else {
  813. i++;
  814. }
  815. }, true)
  816. }, 500);
  817. },
  818. fail: function(res) {
  819. console.log('支付失败')
  820. postRequest('/api/order/cancelPay', {
  821. id: id
  822. }, data => {
  823. uni.redirectTo({
  824. url: '/mine/orderDet/orderDet?id=' + id,
  825. })
  826. })
  827. console.log(res);
  828. }
  829. })
  830. if (isShowLoading) {
  831. uni.hideLoading();
  832. isShowLoading = false;
  833. }
  834. })
  835. // #endif
  836. }
  837. //微信支付订单公共方法 ==活动
  838. const payOrders = (id, success, type) => {
  839. let isShowLoading = false;
  840. if (!isShowLoading) {
  841. loadIng('加载中');
  842. // console.log('加载中')
  843. isShowLoading = true;
  844. }
  845. postRequest('/api/order/weixin/pay', {
  846. id: id
  847. }, json => {
  848. // console.log(json)
  849. payStatus(json, id, success)
  850. uni.requestPayment({
  851. timeStamp: json.timeStamp,
  852. nonceStr: json.nonceStr,
  853. package: json.packages,
  854. signType: json.signType,
  855. paySign: json.sign,
  856. success: function() {
  857. paySuccess(id, success)
  858. },
  859. fail: function(res) {
  860. console.log('支付失败')
  861. postRequest('/api/order/cancelPay', {
  862. id: id
  863. }, data => {
  864. uni.redirectTo({
  865. url: '/mine/activity/activity',
  866. })
  867. })
  868. console.log(res);
  869. }
  870. })
  871. if (isShowLoading) {
  872. uni.hideLoading();
  873. isShowLoading = false;
  874. }
  875. })
  876. }
  877. const authSetting = (authority, success, error) => {
  878. // console.log(authority,success)
  879. // #ifndef H5
  880. uni.getSetting({
  881. success(res) {
  882. if (res.authSetting[authority]) {
  883. success.call(this);
  884. return false;
  885. }
  886. uni.authorize({
  887. scope: authority,
  888. success() {
  889. success.call(this);
  890. },
  891. fail: function(res) {
  892. error.call(this);
  893. }
  894. })
  895. }
  896. })
  897. // #endif
  898. }
  899. const getLocation = (suss) => {
  900. // #ifdef MP-WEIXIN
  901. authSetting('scope.userLocation', () => {
  902. // load('定位中…');
  903. uni.getLocation({
  904. type: 'gcj02',
  905. // isHighAccuracy: true,
  906. success: function(res) {
  907. // uni.hideLoading();
  908. console.log(res, "789")
  909. suss.call(this, res);
  910. },
  911. fail: function(res) {
  912. console.log("调用失败", res)
  913. suss.call(this, 2)
  914. // uni.hideLoading();
  915. }
  916. })
  917. }, () => {
  918. // uni.hideLoading();
  919. console.log("取消")
  920. suss.call(this, 1)
  921. });
  922. // #endif
  923. // #ifdef MP-ALIPAY
  924. uni.getLocation({
  925. type: 'gcj02',
  926. // isHighAccuracy: true,
  927. success: function(res) {
  928. // uni.hideLoading();
  929. console.log(res, "789")
  930. suss.call(this, res);
  931. },
  932. fail: function(res) {
  933. console.log("调用失败", res)
  934. suss.call(this, 2)
  935. // uni.hideLoading();
  936. }
  937. })
  938. // #endif
  939. // #ifdef H5
  940. uni.getLocation({
  941. type: 'gcj02',
  942. isHighAccuracy: true,
  943. success: function(res) {
  944. // uni.hideLoading();
  945. console.log(res)
  946. suss.call(this, res, "789");
  947. },
  948. fail: function() {
  949. console.log("调用失败", res)
  950. suss.call(this, 2)
  951. // uni.hideLoading();
  952. }
  953. })
  954. // #endif
  955. }
  956. const scopeAddress = (success) => {
  957. // #ifdef MP-WEIXIN
  958. authSetting('scope.address', () => {
  959. uni.chooseAddress({
  960. success: function(res) {
  961. success.call(this, res);
  962. },
  963. })
  964. }, () => {
  965. msg('未设置开放权限')
  966. });
  967. // #endif
  968. // #ifdef MP-ALIPAY
  969. uni.chooseAddress({
  970. success: function(res) {
  971. success.call(this, res);
  972. },
  973. })
  974. // #endif
  975. }
  976. const isAuth = () => {
  977. const user = getStorage('userInfo');
  978. return user && user.id;
  979. }
  980. // 去掉字符串中的特殊字符和转义字符
  981. const excludeSpecial = (s) => {
  982. // 去掉转义字符
  983. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  984. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  985. if (s != undefined || s != null) {
  986. s = s.replace(pattern, "")
  987. }
  988. return s;
  989. }
  990. // 行为操作
  991. const saveBehavior = (params, success) => {
  992. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  993. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  994. postRequest('/api/v3/save', params, data => {
  995. success.call(this, data)
  996. })
  997. }
  998. const getBehavior = (params, success) => {
  999. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1000. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1001. console.log('行为')
  1002. getRequest('/api/v3/get', params, data => {
  1003. success.call(this, data)
  1004. })
  1005. }
  1006. /**
  1007. * text:展示导航名
  1008. * iconPath:未点击显示图标
  1009. * selectedIconPath:选中图标
  1010. * pagePath:页面路径
  1011. * channel:匹配名
  1012. * click:点击事件
  1013. * index:排序
  1014. * show:是否显示
  1015. */
  1016. var selectedIconPath
  1017. if (header.appId == 'ZQ1VK5oc17I387E') {
  1018. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'yunyao_h.png'
  1019. } else {
  1020. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'home_h.png'
  1021. }
  1022. var tab = [{
  1023. text: "首页",
  1024. iconPath: "/static/pages/images/home.png",
  1025. selectedIconPath: '/static/pages/images/home_h.png',
  1026. pagePath: 'pages/index/index',
  1027. channel: 'home',
  1028. click: 'goHome',
  1029. index: 1,
  1030. show: true
  1031. },
  1032. {
  1033. text: "活动",
  1034. iconPath: "/static/pages/images/activity.png",
  1035. selectedIconPath: '/static/pages/images/activity_h.png',
  1036. pagePath: 'pages/matchList/index',
  1037. click: 'goMatch',
  1038. channel: 'match',
  1039. index: 2,
  1040. show: true
  1041. },
  1042. {
  1043. text: "社区",
  1044. iconPath: "/static/pages/images/interactive.png",
  1045. selectedIconPath: '/static/pages/images/interactive_h.png',
  1046. pagePath: 'pages/interactive/index',
  1047. click: 'goInteractive',
  1048. channel: 'interactive',
  1049. index: 3,
  1050. show: true
  1051. },
  1052. {
  1053. text: "发现",
  1054. iconPath: "/static/pages/images/found.png",
  1055. selectedIconPath: '/static/pages/images/found_h.png',
  1056. pagePath: 'pages/found/found',
  1057. click: 'goFound',
  1058. channel: 'found',
  1059. index: 3,
  1060. show: true
  1061. },
  1062. {
  1063. text: "我的",
  1064. iconPath: '/static/pages/images/user.png',
  1065. selectedIconPath: '/static/pages/images/user_h.png',
  1066. pagePath: 'pages/user/user',
  1067. click: 'goUser',
  1068. channel: 'user',
  1069. index: 6,
  1070. show: true
  1071. }
  1072. ]
  1073. module.exports = {
  1074. setStorage: setStorage,
  1075. getStorage: getStorage,
  1076. postRequest: postRequest,
  1077. postRequestLoding: postRequestLoding,
  1078. getRequest: getRequest,
  1079. putRequest: putRequest,
  1080. putRequestJson: putRequestJson,
  1081. msg: msg,
  1082. g: g,
  1083. p: p,
  1084. env: env,
  1085. send: send,
  1086. redirectIndex: redirectIndex,
  1087. saveImage: saveImage,
  1088. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1089. uploadFile: uploadFile,
  1090. msgConfirm: msgConfirm,
  1091. getConfig: getConfig,
  1092. load: load,
  1093. isLogin: isLogin,
  1094. isLogins: isLogins,
  1095. payOrder: payOrder,
  1096. payOrders: payOrders,
  1097. getLocation: getLocation,
  1098. scopeAddress: scopeAddress,
  1099. isAuth: isAuth,
  1100. alertMsg: alertMsg,
  1101. getToken: getToken,
  1102. loadIng: loadIng,
  1103. removeStorage: removeStorage,
  1104. public: publics,
  1105. header: header,
  1106. // gettmplIds: gettmplIds,
  1107. excludeSpecial: excludeSpecial,
  1108. tab: tab,
  1109. msgConfirmText: msgConfirmText,
  1110. silenceLogin: silenceLogin,
  1111. getWXUserProfile: getWXUserProfile,
  1112. updataWxInfos: updataWxInfos,
  1113. saveBehavior: saveBehavior,
  1114. getBehavior: getBehavior,
  1115. getIMToken: getIMToken,
  1116. jumpConversation: jumpConversation
  1117. }