request.js 32 KB

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