request.js 25 KB

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