request.js 26 KB

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