request.js 26 KB

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