request.js 27 KB

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