request.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. const utils = require('./util.js');
  2. const h5util = require('./h5util.js');
  3. const api = require('./api.js');
  4. const app = getApp();
  5. // let WebIM = (wx.WebIM = require("../hxChatSDK/utils/WebIM")["default"]);
  6. /**
  7. * H5版的发布地址目录:测试环境
  8. * /data/nginx proxy manager/data/web/xcx_h5
  9. * 域名:http://xcxh5.ts.zhiqiyun.com/
  10. */
  11. const env = {
  12. NODE_ENV: 'dev',
  13. dev: {
  14. apiUrl: 'https://apitest.zhiqiyun.com',
  15. isSolution: false,
  16. shareAppid:'ZQ4KaYIMHTypO0T',
  17. H5_module_url:'http://xcxh5.ts.zhiqiyun.com'
  18. // apiUrl: 'https://apitest.tongyu99.com'
  19. },
  20. prd: {
  21. apiUrl: 'https://apimall.zhiqiyun.com',
  22. isSolution: true,
  23. shareAppid:'ZQCK1g7w7NKYAYP',
  24. H5_module_url:''
  25. },
  26. pota: {
  27. // apiUrl: 'http://192.168.110.59:8098' //f
  28. // apiUrl: 'http://192.168.110.176:8098' ,//yx
  29. apiUrl: 'http://192.168.110.195:8098', //xw
  30. // apiUrl: 'http://192.168.110.182:8098' ,//zf
  31. isSolution: true,
  32. shareAppid:'ZQ4KaYIMHTypO0T',
  33. H5_module_url:''
  34. }
  35. }
  36. var header = {
  37. 'content-type': 'application/x-www-form-urlencoded',
  38. 'appId': 'ZQ4KaYIMHTypO0T', //绿萝云测试
  39. // 'appId': 'ZQKmmjlYyTRmssC', //绿萝云正式
  40. // 'appId': 'ZQDGetYBDwHbfaP',//绿萝大药房测试
  41. // 'appId': 'ZQKmmjlYyTRmssC',//绿萝云正式
  42. // 'appId': 'ZQDGetYBDwHbfaP',//绿萝大药房测试
  43. // 'appId': 'ZQDAvgsQ4P51n1b',//绿萝大药房正式独立租户
  44. // 'appId': 'ZQjtblxKOibbZsE',//绿萝社区诊所测试
  45. // 'appId': 'ZQHGg8rQYqkcKvV',//绿萝中医馆测试
  46. // 'appId': 'ZQP48U6KeLNQZXN',//绿萝医疗美容测试
  47. // 'appId': 'ZQaw2Mgigmwsxwr',//绿萝专科门诊测试
  48. // 'appId': 'ZQrbSJm5S694cXo',//泽惠云测试
  49. // 'appId': 'ZQLyCKPHBHUTgpj',//泽惠云正式
  50. // 'appId': 'ZQYLXwCoEdE13j9',//桃燕禧测试
  51. // 'appId':'ZQuNAVEkY3ClG2D',//桃燕禧正式
  52. // 'appId':'ZQ6XiKauuAoSyfS',//金药堂
  53. // 'appId':'ZQ17g14A1SXrlNU',//本吉口腔
  54. }
  55. /**小程序配置 */
  56. const publics = {
  57. WX_GZH_APPID: 'wx891371f1700a6806',
  58. WX_GZH_URI: 'http://h5.cheese-vip.com/comm/login/login',
  59. 'weixinGzAppid': 'wxf3bf18d2889d98ad', //默认公众号appid
  60. // 'appId': 'ZQCK1g7w7NKYAYP', //正式固定的appid(绿萝云助手运营后台)
  61. 'appId': env[env.NODE_ENV].shareAppid, //测试绿萝云运营平台appid(系统运营租户)
  62. 'H5_module_url':env[env.NODE_ENV].H5_module_url, //测试绿萝云运营平台appid(系统运营租户)
  63. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  64. 'copyrighttitle': '智企云', //底部版权公司名称
  65. 'copyrighttel': '400-698-5980', //底部版权服务电话
  66. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  67. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  68. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  69. 'channel': '', //处方中间渠道号 SAAS系统为空
  70. 'is_customer_pick_up': false,
  71. mapLBSKEY: 'XMCBZ-7K3WN-JD3FA-SDBGN-ZYVU3-MZBQZ',
  72. 'iconSrc': 'blue/',
  73. 'titleTopBgColor': '#1a83ff',
  74. //小程序ID wx987a09d18df068be
  75. //药店版模板小程序:wxddcf0549757d4f61
  76. //中医馆方案模板小程序:wxccea26be900083b6
  77. // 'mapLBSKEY': 'B32BZ-KIP6Q-6IL57-4XOK2-EYOKV-4SBA5',//泽惠云
  78. // 'iconSrc': 'blue/',
  79. // 'titleTopBgColor': '#1a83ff',
  80. //小程序ID wx967c93213cd77e0d
  81. // 桃燕禧小程序ID
  82. // 'mapLBSKEY': 'KWKBZ-BVJWJ-QIGFM-DEITX-56FZ5-OGFSH',
  83. //wxb352884750204cd3
  84. // 金药堂小程序ID
  85. // 'mapLBSKEY': 'LWZBZ-K7WWQ-52L56-2ZCIE-F4PJO-2NBU5',
  86. //wxbb4bc21cf658d44d
  87. // 本吉口腔小程序ID
  88. // 'mapLBSKEY': 'GIKBZ-H6JKT-DLTXX-VO4HD-GPRFJ-7TFHI',
  89. //wxa1ba85dfdafad298
  90. }
  91. function getHeader(content_type, appID) {
  92. var appId = '';
  93. if (appID) {
  94. appId = appID;
  95. } else {
  96. appId = getStorage('appId');
  97. if (!appId) {
  98. appId = header.appId;
  99. setStorage('appId', appId)
  100. }
  101. }
  102. var headers = {
  103. "content-type": content_type ? content_type : 'application/x-www-form-urlencoded',
  104. "appId": appId,
  105. };
  106. return headers;
  107. }
  108. /*
  109. * 获取token
  110. */
  111. const getToken = (success, appId, isKDX) => {
  112. var token = '';
  113. if (isKDX) {
  114. token = getStorage('S_AUTH_TOKEN');
  115. } else {
  116. token = getStorage('AUTH_TOKEN');
  117. }
  118. // let token = 'JKoXWFCY1yB93BQdLVb9BcibWAaexceyR';
  119. if (token) {
  120. success.call(this, token);
  121. return false;
  122. } else {
  123. loadToken(data => {
  124. success.call(this, data);
  125. }, appId, isKDX);
  126. }
  127. }
  128. const loadToken = (success, appId, isKDX) => {
  129. let that = this
  130. uni.request({
  131. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  132. data: {},
  133. method: 'get',
  134. header: getHeader('', appId),
  135. success(json) {
  136. if (json.statusCode !== 200) return msg(json.data.message);
  137. if (json.data.code !== 0) return msg(json.data.msg)
  138. if (isKDX) {
  139. setStorage('S_AUTH_TOKEN', json.data.data);
  140. } else {
  141. setStorage('AUTH_TOKEN', json.data.data);
  142. }
  143. success.call(that, json.data.data);
  144. },
  145. fail() {
  146. msg('access_token 获取失败');
  147. }
  148. })
  149. }
  150. /**
  151. * 微信小程序静默授权登录
  152. * @param {*} parentId
  153. * @param {*} merchantId
  154. * @param {*} type 用户来源类型 1 小程序搜索 2 活动报名 3 名片分享 4商品分享 5店铺分享 6文章分享 7小程序分享 8素材分享 9公众号分享 10公众号搜索 11企微活码 12微信活码 13手工录入 14 5G消息进入
  155. */
  156. const silenceLogin = (parentId, merchantId, type) => {
  157. return new Promise(async(resolve, reject) => {
  158. let logins =await isAuthFn(false)
  159. if (!logins) { //未登录
  160. // #ifdef MP-WEIXIN || MP-TOUTIAO
  161. uni.login({
  162. success(res) {
  163. console.log('静默授权登录>>>', res);
  164. let params = {
  165. code: res.code,
  166. parentId: parentId ? parentId : '',
  167. merchantId: merchantId ? merchantId : ''
  168. }
  169. var url = '';
  170. // #ifdef MP-WEIXIN
  171. url = '/api/v3/silence/login';
  172. // #endif
  173. // #ifdef MP-TOUTIAO
  174. url = '/api/v3/tiktok/login';
  175. // #endif
  176. postRequest(url, params, data => {
  177. if (data.token) setStorage('AUTH_TOKEN', data.token);
  178. setStorage('userInfo', data);
  179. savePortAndSource({
  180. uid: getStorage('userInfo').id,
  181. source: type
  182. })
  183. // getRequest('/api/config',{},data=>{
  184. // if(data.is_not_open_im != 1) getIMToken(false);
  185. // })
  186. resolve()
  187. });
  188. }
  189. });
  190. // #endif
  191. // #ifdef H5
  192. //判断是否为微信内环境,只有内环境才执行静默授权登录
  193. // env.NODE_ENV=='prd' 只有正式环境域名可静默授权,如果是测试环境仍用手机号登陆
  194. if (utils.isWeixin() && env.NODE_ENV=='prd') {
  195. console.log('$$$$$$$$微信环境,进入静默授权')
  196. getApp().wxOauth(parentId, merchantId, 'snsapi_base');
  197. resolve()
  198. }else{
  199. resolve()
  200. }
  201. // #endif
  202. } else {
  203. // getRequest('/api/config',{},data=>{
  204. // if(data.is_not_open_im != 1) getIMToken(false);
  205. // })
  206. // initIMChat();
  207. savePortAndSource({
  208. uid: getStorage('userInfo').id,
  209. source: type
  210. })
  211. resolve()
  212. }
  213. })
  214. }
  215. const onNetworkStatusChangeIM = () => {
  216. // console.log('IM>>>>>>>>>>>>>网络变化调用');
  217. // uni.onNetworkStatusChange(info => {
  218. // msg('切换了网络,开始重连');
  219. // WebIM.conn.close();
  220. // setTimeout(() => {
  221. // initIMChat();
  222. // }, 500);
  223. // });
  224. }
  225. /**
  226. * 初始化 IMsdk
  227. */
  228. const initIMChat = () => {
  229. var user = getStorage('userInfo');
  230. let myUsername = uni.getStorageSync("myUsername")
  231. var imToken = getStorage('imToken');
  232. console.log(imToken)
  233. if (imToken) {
  234. if (myUsername) {
  235. getApp().globalData.conn.open({
  236. apiUrl: WebIM.config.apiURL,
  237. user: myUsername,
  238. pwd: getStorage('imToken'),
  239. accessToken: getStorage('imToken'),
  240. appKey: WebIM.config.appkey
  241. });
  242. } else {
  243. getRequest('/api/v3/im/em/userId', {
  244. userId: user.id
  245. }, suc => {
  246. uni.setStorage({
  247. key: "myUsername",
  248. data: 'RS_' + suc
  249. });
  250. getApp().globalData.conn.open({
  251. apiUrl: WebIM.config.apiURL,
  252. user: 'RS_' + suc,
  253. pwd: getStorage('imToken'),
  254. accessToken: getStorage('imToken'),
  255. appKey: WebIM.config.appkey
  256. });
  257. });
  258. }
  259. } else {
  260. getRequest('/api/v3/im/em/userToken', {}, suc => {
  261. setStorage('imToken', suc.token);
  262. uni.setStorage({
  263. key: "myUsername",
  264. data: 'RS_' + suc.id
  265. });
  266. getApp().globalData.conn.open({
  267. apiUrl: WebIM.config.apiURL,
  268. user: suc.id,
  269. pwd: getStorage('imToken'),
  270. accessToken: getStorage('imToken'),
  271. appKey: WebIM.config.appkey
  272. });
  273. });
  274. }
  275. }
  276. const getIMToken = (isUpdatToken) => {
  277. var user = getStorage('userInfo');
  278. getRequest('/api/v3/im/em/userToken', {}, suc => {
  279. setStorage('imToken', suc.token);
  280. uni.setStorage({
  281. key: "myUsername",
  282. data: 'RS_' + suc.id
  283. });
  284. if (isUpdatToken) {
  285. //更新token
  286. getApp().globalData.conn.open({
  287. user: 'RS_' + suc.id,
  288. accessToken: getStorage('imToken'),
  289. });
  290. getApp().globalData.onLoginSuccess(uni.getStorageSync("myUsername"));
  291. } else {
  292. // initIMChat();
  293. }
  294. }, true);
  295. }
  296. /**
  297. * 跳转进入消息列表
  298. */
  299. const jumpConversation = () => {
  300. if (!isLogins(true)) { //未登录
  301. return;
  302. }
  303. getIMToken(true);
  304. }
  305. /**
  306. * 跳入聊天
  307. * @param {Object} userId 对方用户ID
  308. * @param {Object} avatar 对方用户头像
  309. * @param {Object} realName 对方用户名称
  310. */
  311. const jumpSingleChatRoom = function(userId, avatar, realName) {
  312. getRequest('/manage/v3/im/em/userId', {
  313. id: userId
  314. }, suc => {
  315. var my = uni.getStorageSync("myUsername");
  316. var nameList = {
  317. myName: my,
  318. your: 'rs_' + suc
  319. };
  320. uni.setStorage({
  321. key: 'imUserInfo_' + nameList.your,
  322. data: {
  323. avatar: avatar,
  324. id: userId,
  325. nickName: realName
  326. }
  327. });
  328. uni.navigateTo({
  329. url: "/chat/chatroom/chatroom?username=" + JSON.stringify(nameList)
  330. });
  331. });
  332. }
  333. const getWXUserProfile = (suc) => {
  334. let that = this;
  335. var sessionKey = '';
  336. var openid = '';
  337. getApp().globalData.getCheckSessoin(json => {
  338. sessionKey = json.session_key;
  339. openid = json.openid;
  340. });
  341. try {
  342. uni.getUserProfile({
  343. desc: '完善信息',
  344. success: res => {
  345. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  346. },
  347. fail: res => {
  348. console.log('授权失败数据>>>', res);
  349. msg(res);
  350. }
  351. });
  352. } catch (e) {
  353. // 老版
  354. wx.getUserInfo({
  355. success: function(ress) {
  356. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  357. }
  358. });
  359. }
  360. }
  361. /**
  362. * 保存微信头像
  363. */
  364. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  365. if (!encryptedData || !iv) return false;
  366. let _params = {
  367. sessionKey: sessionKey,
  368. openId: openid,
  369. iv: iv,
  370. encryptedData: encryptedData
  371. };
  372. postRequest('/api/user/weixin/update', _params, json => {
  373. setStorage('userInfo', json);
  374. suc.call();
  375. });
  376. }
  377. const clearValueEmpty = (data) => {
  378. let keyValue = {};
  379. for (let key in data) {
  380. let value = typeof data[key];
  381. if (value == 'string' && value) {
  382. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] !=
  383. null) {
  384. keyValue[key] = data[key];
  385. }
  386. // keyValue[key] = data[key];
  387. } else if (value == 'number' && value != null) {
  388. keyValue[key] = data[key];
  389. } else if (value == 'boolean') {
  390. keyValue[key] = data[key];
  391. } else {
  392. if (data[key]) keyValue[key] = data[key];
  393. }
  394. }
  395. return keyValue;
  396. }
  397. /**
  398. * 公共请求
  399. *
  400. * @param {*} url 请求url
  401. * @param {*} data 请求参数
  402. * @param {*} method 请求方法
  403. * @param {*} success 成功函数
  404. * @param {*} isLoad 是否显示加载提示
  405. */
  406. const baseRequest = (url, data, method, success, isLoad) => {
  407. getToken(token => {
  408. if (isLoad) {
  409. loadIng('加载中');
  410. }
  411. let headers = '';
  412. if (url.indexOf('/v3') != -1) {
  413. headers = getHeader('application/json;charset=UTF-8', '');
  414. } else {
  415. headers = getHeader('application/x-www-form-urlencoded', '');
  416. }
  417. headers.authorityToken = token;
  418. uni.request({
  419. url: env[env.NODE_ENV].apiUrl + url,
  420. data: clearValueEmpty(data),
  421. method: method,
  422. header: headers,
  423. success(json) {
  424. if (isLoad) {
  425. uni.hideLoading();
  426. }
  427. if (json.statusCode !== 200) return msg(json.data.message);
  428. if (json.data.code === 10021 || json.data.code === 10020) {
  429. // console.log("第二次执行")
  430. if(url=='/api/isAuth'){
  431. success.call(this, json.data.data);
  432. return false
  433. }else{
  434. removeStorage('AUTH_TOKEN');
  435. return redirectIndex();
  436. }
  437. }
  438. if (json.data.code !== 0) {
  439. return msg(json.data.msg)
  440. }
  441. // console.log("第二次执行")
  442. let data = json.data.data;
  443. if (typeof data === 'string' && data.indexOf("{") === 0) {
  444. data = JSON.parse(data);
  445. }
  446. success.call(this, data);
  447. },
  448. fail() {
  449. if (isLoad) {
  450. uni.hideLoading();
  451. }
  452. },
  453. // complete(){
  454. // success.call(this, data);
  455. // },
  456. })
  457. })
  458. }
  459. const loadIng = (msg) => {
  460. uni.showLoading({
  461. title: msg,
  462. mask: true
  463. })
  464. }
  465. /**POST请求 */
  466. const postRequest = (url, data, success, isLoad) => {
  467. header['content-type'] = 'application/x-www-form-urlencoded'
  468. baseRequest(url, data, 'post', success, isLoad);
  469. }
  470. const postRequestLoding = (url, data, success, isLoding) => {
  471. header['content-type'] = 'application/x-www-form-urlencoded'
  472. baseRequest(url, data, 'post', success, false, isLoding);
  473. }
  474. /**GET请求 */
  475. const getRequest = (url, data, success, isLoad) => {
  476. header['content-type'] = 'application/x-www-form-urlencoded'
  477. baseRequest(url, data, 'get', success, isLoad);
  478. }
  479. /**Put请求 */
  480. const putRequest = (url, data, success, isLoad) => {
  481. header['content-type'] = 'application/x-www-form-urlencoded'
  482. baseRequest(url, data, 'put', success, isLoad)
  483. }
  484. const putRequestJson = (url, data, success, isLoad) => {
  485. header['content-type'] = 'application/json;charset=UTF-8'
  486. baseRequest(url, data, 'put', success, isLoad)
  487. }
  488. /**public请求 */
  489. const getRequestKDX = (url, data, success, isLoad) => {
  490. baseRequestKDX(url, data, 'get', success, isLoad, publics.appId);
  491. }
  492. /**KDXPOST请求 */
  493. const postRequestKDX = (url, data, success, isLoad) => {
  494. baseRequestKDX(url, data, 'post', success, isLoad, publics.appId);
  495. }
  496. /**
  497. * 公共请求
  498. *
  499. * @param {*} url 请求url
  500. * @param {*} data 请求参数
  501. * @param {*} method 请求方法
  502. * @param {*} success 成功函数
  503. * @param {*} isLoad 是否显示加载提示
  504. */
  505. const baseRequestKDX = (url, data, method, success, isLoad, appId) => {
  506. getToken(token => {
  507. if (isLoad) {
  508. uni.showLoading({
  509. title: '加载中',
  510. mask: true
  511. })
  512. }
  513. var headerKDX = {};
  514. headerKDX.authorityToken = token;
  515. headerKDX.appId = appId;
  516. if (url.indexOf('/v3') != -1) {
  517. headerKDX['content-type'] = 'application/json;charset=UTF-8'
  518. } else {
  519. headerKDX['content-type'] = 'application/x-www-form-urlencoded'
  520. }
  521. console.log('headerKDX==', headerKDX)
  522. uni.request({
  523. url: env[env.NODE_ENV].apiUrl + url,
  524. data: clearValueEmpty(data),
  525. method: method,
  526. header: headerKDX,
  527. success(json) {
  528. if (!isLoad) uni.hideNavigationBarLoading();
  529. uni.hideLoading();
  530. if (json.statusCode !== 200) return msg(json.data.message);
  531. if (json.data.code === 10021 || json.data.code === 10020) {
  532. if (toLogin) {
  533. toLogin = false;
  534. console.log("执行登录")
  535. return redirectIndex();
  536. } else {
  537. console.log("不执行登录")
  538. return;
  539. }
  540. }
  541. toLogin = true;
  542. if (json.data.code !== 0) {
  543. return msg(json.data.msg)
  544. }
  545. let data = json.data.data;
  546. if (typeof data === 'string' && data.indexOf("{") === 0) {
  547. data = JSON.parse(data);
  548. }
  549. success.call(this, data);
  550. },
  551. fail() {
  552. if (!isLoad) uni.hideNavigationBarLoading();
  553. uni.hideLoading();
  554. }
  555. })
  556. }, appId, true)
  557. }
  558. /**
  559. * 上传文件
  560. * @param {*} url 请求url
  561. * @param {*} data 携带数据
  562. * @param {*} success 请求成功函数
  563. */
  564. const uploadFile = (url, data, success) => {
  565. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  566. getToken(token => {
  567. uni.uploadFile({
  568. url: env[env.NODE_ENV].apiUrl + url,
  569. filePath: data,
  570. name: 'file',
  571. header: {
  572. 'appId': getStorage('appId') ? getStorage('appId') : header['appId'],
  573. 'authorityToken': token
  574. },
  575. success(res) {
  576. if (res.statusCode !== 200) return msg('文件上传失败');
  577. let data = JSON.parse(res.data);
  578. if (data.code !== 0) return msg(data.msg);
  579. success.call(this, data.data);
  580. },
  581. })
  582. })
  583. }
  584. /**弹窗 */
  585. const msg = (title, success) => {
  586. if (title) {
  587. uni.showToast({
  588. title: title,
  589. icon: 'none',
  590. success() {
  591. if (success) success.call(this);
  592. }
  593. });
  594. }
  595. return false;
  596. }
  597. const msgConfirm = (msg, success, cancel) => {
  598. uni.showModal({
  599. title: '提示',
  600. content: msg,
  601. success(res) {
  602. if (res.confirm) {
  603. success.call(this);
  604. } else {
  605. if (cancel) cancel.call(this);
  606. }
  607. }
  608. })
  609. }
  610. const msgConfirmText = (msg, confirmText, success, cancel) => {
  611. uni.showModal({
  612. title: '提示',
  613. content: msg,
  614. confirmText: confirmText,
  615. success(res) {
  616. if (res.confirm) {
  617. success.call(this);
  618. } else {
  619. if (cancel) cancel.call(this);
  620. }
  621. }
  622. })
  623. }
  624. const alertMsg = (msg, success) => {
  625. uni.showModal({
  626. title: '提示',
  627. content: msg,
  628. showCancel: false,
  629. success(res) {
  630. if (success) success.call(this);
  631. }
  632. })
  633. return false;
  634. }
  635. const load = msg => {
  636. uni.showLoading({
  637. title: msg,
  638. mask: true
  639. })
  640. }
  641. const g = (url, success, isLoad) => {
  642. getRequest(url, {}, success, isLoad)
  643. }
  644. const p = (url, success, isLoad) => {
  645. postRequest(url, {}, success, isLoad)
  646. }
  647. const send = (url, mobile, success, error) => {
  648. if (!mobile) {
  649. return msg('手机号码不能为空', error);
  650. }
  651. postRequest(url, {
  652. phone: mobile
  653. }, json => {
  654. if (json.code !== 0) {
  655. return msg(json.msg, error);
  656. }
  657. let time = 60;
  658. const initTime = setInterval(() => {
  659. time--;
  660. if (time > 0) {
  661. success.call(this, time + '秒后获取');
  662. } else {
  663. clearInterval(initTime);
  664. success.call(this, '获取验证码');
  665. }
  666. }, 1000)
  667. })
  668. }
  669. // 登陆拦截
  670. const redirectIndex = (params) => {
  671. // uni.clearStorageSync();
  672. removeStorage('userInfo');
  673. // console.log(params)
  674. var pages = getCurrentPages();
  675. // console.log("pages》》》》》登陆拦截", pages);
  676. let currentUrl;
  677. if (pages) {
  678. var currentPage = pages[pages.length - 1];
  679. if (currentPage) {
  680. currentUrl = currentPage.route;
  681. // let query = currentPage.data.query;
  682. // if (query) {
  683. // for (let key in query) {
  684. // const fo = key + '=' + query[key];
  685. // currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  686. // }
  687. // }
  688. if (currentUrl) {
  689. console.log('currentUrl》》》》》', currentUrl);
  690. setStorage("REDIRECT_URL", '/' + currentUrl);
  691. // #ifndef MP-WEIXIN
  692. if (currentUrl == 'pages/applogin/index') {
  693. console.log('当前已经处于登录页面,不在向下执行跳转');
  694. return
  695. }
  696. // #endif
  697. // #ifdef MP-WEIXIN
  698. if (currentUrl == 'pages/authorize/authorize') {
  699. console.log('当前已经处于登录页面,不在向下执行跳转');
  700. return
  701. }
  702. // #endif
  703. }
  704. }
  705. }
  706. var redirectUrl = '';
  707. //小程序环境设置
  708. var ua = window.navigator.userAgent.toLowerCase();
  709. //判断是否是微信环境
  710. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  711. jWeixin.miniProgram.getEnv((res) => {
  712. if(res.miniprogram){
  713. redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  714. }else{
  715. redirectUrl = '/pages/applogin/index' + (params ? params : '');
  716. }
  717. })
  718. }else{
  719. redirectUrl = '/pages/applogin/index' + (params ? params : '');
  720. }
  721. var jump = getStorage('authorize_jump') //以下解决多次跳转登录页的重点
  722. if (!jump || currentUrl !== 'pages/authorize/authorize') {
  723. //以下做token失效的操作
  724. setTimeout(() => {
  725. // uni.navigateTo({
  726. // url: redirectUrl
  727. // })
  728. h5util.jumpUrlExternal({
  729. url:redirectUrl,
  730. onlyWeb:false
  731. })
  732. }, 100)
  733. setStorage('authorize_jump', 'true')
  734. }
  735. return false;
  736. }
  737. const saveImage = (url) => {
  738. uni.saveImageToPhotosAlbum({
  739. filePath: url,
  740. success() {
  741. msg('图片保存成功');
  742. }
  743. })
  744. }
  745. const saveImageToPhotosAlbum = (url) => {
  746. if (!url) return msg('小程序码不存在');
  747. uni.getImageInfo({
  748. src: url,
  749. success(json) {
  750. uni.getSetting({
  751. success(res) {
  752. if (!res.authSetting['scope.writePhotosAlbum']) {
  753. uni.authorize({
  754. scope: 'scope.writePhotosAlbum',
  755. success() {
  756. saveImage(json.path);
  757. }
  758. })
  759. } else {
  760. saveImage(json.path);
  761. }
  762. }
  763. })
  764. }
  765. })
  766. }
  767. const getConfig = (config, id) => {
  768. let con = {};
  769. config.info.forEach(inf => {
  770. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  771. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  772. con[inf.setName] = inf.setDefault;
  773. });
  774. config.card.forEach(inf => {
  775. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  776. con[inf.setName] = inf.setDefault;
  777. });
  778. config.realTime.forEach(inf => {
  779. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  780. con[inf.setName] = inf.setDefault;
  781. });
  782. return con;
  783. }
  784. const isLogin = async (error) => {
  785. let userInfo = getStorage('userInfo');
  786. if (!userInfo || !userInfo.id) {
  787. if (error) return false;
  788. // console.log("第一次执行")
  789. return redirectIndex();
  790. }
  791. return true;
  792. }
  793. //检测是否登录
  794. const isLogins = async(isGoLogin) => {
  795. let userInfo = getStorage('userInfo');
  796. if (!userInfo) {
  797. if (isGoLogin) {
  798. redirectIndex();
  799. }
  800. return false;
  801. }
  802. return true;
  803. }
  804. //检测是否登录接口形式
  805. const isAuthFn = (isGoLogin)=>{
  806. return new Promise((r,j)=>{
  807. postRequest('/api/isAuth',{},res=>{
  808. if(!res){
  809. if (isGoLogin) {
  810. redirectIndex();
  811. }
  812. }
  813. r(res?true:false)
  814. })
  815. })
  816. }
  817. const setStorage = (key, value) => {
  818. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  819. }
  820. const getStorage = (key) => {
  821. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  822. }
  823. const removeStorage = (key) => {
  824. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  825. }
  826. /**
  827. * 缓存模板id
  828. */
  829. // const gettmplIds = () => {
  830. // getRequest('/api/wxmes/querylist', {}, res => {
  831. // setStorage('tmplIds', res)
  832. // console.log(res)
  833. // })
  834. // }
  835. //微信支付订单公共方法
  836. const payOrder = (id, success, type) => {
  837. var loctionAddressMap = getStorage('loctionAddressMap');
  838. var datas = {
  839. id: id
  840. };
  841. // #ifdef H5
  842. datas.payChannel = 'wx_mweb';
  843. // #endif
  844. if (loctionAddressMap) {
  845. datas.province = loctionAddressMap.province;
  846. datas.city = loctionAddressMap.city;
  847. };
  848. if (getStorage('isVideoScene')) {
  849. uni.checkBeforeAddOrder({
  850. success(res) {
  851. console.log('下单前置检查', res)
  852. datas.sceneCheckStr = JSON.stringify(res.data);
  853. wexinPay(datas, success, type)
  854. },
  855. })
  856. } else {
  857. wexinPay(datas, success, type)
  858. }
  859. }
  860. //支付中或已支付
  861. const payStatus = (json, id, success) => {
  862. if (json && json.code === 115) {
  863. setTimeout(res => {
  864. uni.hideLoading();
  865. isShowLoading = false;
  866. uni.showModal({
  867. title: '提示',
  868. content: '当前订单正在支付或30秒后重试',
  869. confirmText: '我知道了',
  870. showCancel: false,
  871. success(res) {}
  872. })
  873. }, 3000)
  874. return false;
  875. }
  876. if (json.type === 2 || json.type === 3) {
  877. //调用后台判断订单是否支付成功
  878. let i = 0;
  879. let timer = setInterval(() => {
  880. postRequest('/api/order/check', {
  881. id: id
  882. }, res => {
  883. if (res.isSuccess || i === 4) {
  884. clearInterval(timer);
  885. success.call(this, res.isSuccess);
  886. } else {
  887. i++;
  888. }
  889. }, true)
  890. }, 200);
  891. // success.call(this);
  892. return false;
  893. }
  894. }
  895. // 支付成功回调
  896. const paySuccess = (id, success) => {
  897. console.log('支付成功')
  898. let i = 0;
  899. let timer = setInterval(() => {
  900. postRequest('/api/order/check', {
  901. id: id
  902. }, res => {
  903. console.log(i)
  904. if (res.isSuccess || i === 5) {
  905. clearInterval(timer);
  906. success.call(this, res.isSuccess);
  907. } else {
  908. i++;
  909. }
  910. }, true)
  911. }, 1000);
  912. }
  913. // 支付失败回调
  914. const payFail = (id, type) => {
  915. console.log('支付失败')
  916. postRequest('/api/order/cancelPay', {
  917. id: id
  918. }, data => {
  919. if (type == 'payment') return false;
  920. uni.redirectTo({
  921. url: '/mine/orderDet/orderDet?id=' + id,
  922. })
  923. })
  924. }
  925. //微信支付订单公共方法 ==活动
  926. const payOrders = (id, success, type) => {
  927. let isShowLoading = false;
  928. if (!isShowLoading) {
  929. loadIng('加载中');
  930. // console.log('加载中')
  931. isShowLoading = true;
  932. }
  933. postRequest('/api/order/weixin/pay', {
  934. id: id
  935. }, json => {
  936. // console.log(json)
  937. payStatus(json, id, success)
  938. uni.requestPayment({
  939. timeStamp: json.timeStamp,
  940. nonceStr: json.nonceStr,
  941. package: json.packages,
  942. signType: json.signType,
  943. paySign: json.sign,
  944. success: function() {
  945. paySuccess(id, success)
  946. },
  947. fail: function(res) {
  948. console.log('支付失败')
  949. postRequest('/api/order/cancelPay', {
  950. id: id
  951. }, data => {
  952. if (type == 'registered') {
  953. uni.redirectTo({
  954. url: '/onlineregistration/detail/detail?id=' + id,
  955. })
  956. } else {
  957. uni.redirectTo({
  958. url: '/match/activitys/activity',
  959. })
  960. }
  961. })
  962. console.log(res);
  963. }
  964. })
  965. if (isShowLoading) {
  966. uni.hideLoading();
  967. isShowLoading = false;
  968. }
  969. })
  970. }
  971. const wexinPay = (datas, success, type) => {
  972. let isShowLoading = false;
  973. if (!isShowLoading) {
  974. loadIng('加载中');
  975. // console.log('加载中')
  976. isShowLoading = true;
  977. }
  978. // #ifdef MP-WEIXIN
  979. postRequest('/api/order/weixin/pay', datas, json => {
  980. // console.log('json',json)
  981. payStatus(json, datas.id, success, type)
  982. if (getStorage('isVideoScene')) {
  983. console.log('拉起收银台')
  984. wx.requestOrderPayment({
  985. timeStamp: json.timeStamp,
  986. nonceStr: json.nonceStr,
  987. package: json.packages,
  988. signType: json.signType,
  989. orderInfo: json.orderInfo,
  990. paySign: json.sign,
  991. success: function() {
  992. paySuccess(datas.id, success)
  993. },
  994. fail: function(res) {
  995. console.log(res);
  996. payFail(datas.id, type)
  997. }
  998. })
  999. } else {
  1000. uni.requestPayment({
  1001. timeStamp: json.timeStamp,
  1002. nonceStr: json.nonceStr,
  1003. package: json.packages,
  1004. signType: json.signType,
  1005. paySign: json.sign,
  1006. success: function() {
  1007. paySuccess(datas.id, success)
  1008. },
  1009. fail: function(res) {
  1010. console.log(res);
  1011. payFail(datas.id, type)
  1012. }
  1013. })
  1014. }
  1015. if (isShowLoading) {
  1016. uni.hideLoading();
  1017. isShowLoading = false;
  1018. }
  1019. })
  1020. // #endif
  1021. // #ifdef MP-ALIPAY
  1022. datas.payChannel = 'alipay_lite ';
  1023. postRequest('/api/order/weixin/pay', datas, json => {
  1024. // console.log(json)
  1025. if (json.type === 2 || json.type === 3) {
  1026. //调用后台判断订单是否支付成功
  1027. let i = 0;
  1028. let timer = setInterval(() => {
  1029. postRequest('/api/order/check', {
  1030. id: id
  1031. }, res => {
  1032. if (res.isSuccess || i === 4) {
  1033. clearInterval(timer);
  1034. success.call(this, res.isSuccess);
  1035. } else {
  1036. i++;
  1037. }
  1038. }, true)
  1039. }, 200);
  1040. // success.call(this);
  1041. return false;
  1042. }
  1043. uni.requestPayment({
  1044. provider: 'alipay',
  1045. orderInfo: json.packages,
  1046. success: function() {
  1047. paySuccess(datas.id, success)
  1048. },
  1049. fail: function(res) {
  1050. payFail(datas.id)
  1051. console.log(res);
  1052. }
  1053. })
  1054. if (isShowLoading) {
  1055. uni.hideLoading();
  1056. isShowLoading = false;
  1057. }
  1058. })
  1059. // #endif
  1060. // #ifdef H5
  1061. postRequest('/api/order/weixin/pay', datas, json => {
  1062. // console.log(json)
  1063. if (json.type === 2 || json.type === 3) {
  1064. //调用后台判断订单是否支付成功
  1065. paySuccess(datas.id, success);
  1066. // success.call(this);
  1067. return false;
  1068. }
  1069. if (json.channel === 9 || json.channel === 2) {
  1070. //调用后台判断订单是否支付成功
  1071. window.location.href = json.payUrl;
  1072. return false;
  1073. }
  1074. if (isShowLoading) {
  1075. uni.hideLoading();
  1076. isShowLoading = false;
  1077. }
  1078. })
  1079. // #endif
  1080. }
  1081. //微信支付订单公共方法
  1082. const payOrdersss = (id, success) => {
  1083. let isShowLoading = false;
  1084. if (!isShowLoading) {
  1085. loadIng('加载中');
  1086. // console.log('加载中')
  1087. isShowLoading = true;
  1088. }
  1089. var loctionAddressMap = getStorage('loctionAddressMap');
  1090. var datas = {
  1091. id: id
  1092. };
  1093. if (loctionAddressMap) {
  1094. datas.province = loctionAddressMap.province;
  1095. datas.city = loctionAddressMap.city;
  1096. };
  1097. // #ifdef MP-WEIXIN
  1098. postRequest('/api/order/weixin/pay', datas, json => {
  1099. // console.log(json)
  1100. if (json.type === 2 || json.type === 3) {
  1101. //调用后台判断订单是否支付成功
  1102. let i = 0;
  1103. let timer = setInterval(() => {
  1104. postRequest('/api/order/check', {
  1105. id: id
  1106. }, res => {
  1107. if (res.isSuccess || i === 4) {
  1108. clearInterval(timer);
  1109. success.call(this, res.isSuccess);
  1110. } else {
  1111. i++;
  1112. }
  1113. }, true)
  1114. }, 200);
  1115. // success.call(this);
  1116. return false;
  1117. }
  1118. uni.requestPayment({
  1119. timeStamp: json.timeStamp,
  1120. nonceStr: json.nonceStr,
  1121. package: json.packages,
  1122. signType: json.signType,
  1123. paySign: json.sign,
  1124. success: function() {
  1125. console.log('支付成功')
  1126. let i = 0;
  1127. let timer = setInterval(() => {
  1128. postRequest('/api/order/check', {
  1129. id: id
  1130. }, res => {
  1131. console.log(i)
  1132. if (res.isSuccess || i === 5) {
  1133. clearInterval(timer);
  1134. success.call(this, res.isSuccess);
  1135. } else {
  1136. i++;
  1137. }
  1138. }, true)
  1139. }, 500);
  1140. },
  1141. fail: function(res) {
  1142. console.log('支付失败')
  1143. postRequest('/api/order/cancelPay', {
  1144. id: id
  1145. }, data => {
  1146. uni.redirectTo({
  1147. url: '/mine/orderDet/orderDet?id=' + id,
  1148. })
  1149. })
  1150. console.log(res);
  1151. }
  1152. })
  1153. if (isShowLoading) {
  1154. uni.hideLoading();
  1155. isShowLoading = false;
  1156. }
  1157. })
  1158. // #endif
  1159. // #ifdef MP-ALIPAY
  1160. datas.payChannel = 'alipay_lite ';
  1161. postRequest('/api/order/weixin/pay', datas, json => {
  1162. // console.log(json)
  1163. if (json.type === 2 || json.type === 3) {
  1164. //调用后台判断订单是否支付成功
  1165. let i = 0;
  1166. let timer = setInterval(() => {
  1167. postRequest('/api/order/check', {
  1168. id: id
  1169. }, res => {
  1170. if (res.isSuccess || i === 4) {
  1171. clearInterval(timer);
  1172. success.call(this, res.isSuccess);
  1173. } else {
  1174. i++;
  1175. }
  1176. }, true)
  1177. }, 200);
  1178. // success.call(this);
  1179. return false;
  1180. }
  1181. uni.requestPayment({
  1182. provider: 'alipay',
  1183. orderInfo: json.packages,
  1184. success: function() {
  1185. console.log('支付成功')
  1186. let i = 0;
  1187. let timer = setInterval(() => {
  1188. postRequest('/api/order/check', {
  1189. id: id
  1190. }, res => {
  1191. console.log(i)
  1192. if (res.isSuccess || i === 5) {
  1193. clearInterval(timer);
  1194. success.call(this, res.isSuccess);
  1195. } else {
  1196. i++;
  1197. }
  1198. }, true)
  1199. }, 500);
  1200. },
  1201. fail: function(res) {
  1202. console.log('支付失败')
  1203. postRequest('/api/order/cancelPay', {
  1204. id: id
  1205. }, data => {
  1206. uni.redirectTo({
  1207. url: '/mine/orderDet/orderDet?id=' + id,
  1208. })
  1209. })
  1210. console.log(res);
  1211. }
  1212. })
  1213. if (isShowLoading) {
  1214. uni.hideLoading();
  1215. isShowLoading = false;
  1216. }
  1217. })
  1218. // #endif
  1219. }
  1220. /**
  1221. * type 4为余额充值
  1222. */
  1223. const payAppOrder = (id, openId, type, success) => {
  1224. let isShowLoading = false;
  1225. if (!isShowLoading) {
  1226. loadIng('加载中');
  1227. isShowLoading = true;
  1228. }
  1229. var loctionAddressMap = getStorage('loctionAddressMap');
  1230. var datas = {
  1231. id: id,
  1232. openId: openId
  1233. };
  1234. if (loctionAddressMap) {
  1235. datas.province = loctionAddressMap.province;
  1236. datas.city = loctionAddressMap.city;
  1237. };
  1238. var url = '';
  1239. if (type == 4) {
  1240. url = '/api/members/v2/weixin/pay';
  1241. datas.status = type;
  1242. } else {
  1243. url = '/api/order/v2/weixin/pay';
  1244. }
  1245. postRequest(url, datas, json => {
  1246. var urlapi = '';
  1247. var dataP = {};
  1248. if (type == 4) {
  1249. urlapi = '/api/rrLogs/check'
  1250. dataP.logsId = id;
  1251. } else {
  1252. urlapi = '/api/order/v2/check'
  1253. dataP.id = id;
  1254. }
  1255. if (json.type === 2 || json.type === 3) {
  1256. //调用后台判断订单是否支付成功
  1257. let i = 0;
  1258. let timer = setInterval(() => {
  1259. postRequest(urlapi, dataP, res => {
  1260. if (res.isSuccess || i === 4) {
  1261. clearInterval(timer);
  1262. success.call(this, res.isSuccess);
  1263. } else {
  1264. i++;
  1265. }
  1266. }, true)
  1267. }, 200);
  1268. return false;
  1269. }
  1270. uni.requestPayment({
  1271. timeStamp: json.timeStamp,
  1272. nonceStr: json.nonceStr,
  1273. package: json.packages,
  1274. signType: json.signType,
  1275. paySign: json.sign,
  1276. success: function() {
  1277. let i = 0;
  1278. loadIng('加载中');
  1279. let timer = setInterval(() => {
  1280. postRequest(urlapi, dataP, res => {
  1281. console.log(i)
  1282. if (res.isSuccess || i === 5) {
  1283. clearInterval(timer);
  1284. uni.hideLoading();
  1285. success.call(this, res.isSuccess);
  1286. } else {
  1287. i++;
  1288. }
  1289. })
  1290. }, 500);
  1291. },
  1292. fail: function(res) {
  1293. success.call(this, false);
  1294. msg('支付失败')
  1295. }
  1296. })
  1297. if (isShowLoading) {
  1298. uni.hideLoading();
  1299. isShowLoading = false;
  1300. }
  1301. });
  1302. }
  1303. const receipt = (id, success) => {
  1304. let isShowLoading = false;
  1305. if (!isShowLoading) {
  1306. loadIng('加载中');
  1307. // console.log('加载中')
  1308. isShowLoading = true;
  1309. }
  1310. postRequest('/api/receipt/weixin/pay', {
  1311. id: id
  1312. }, json => {
  1313. // console.log(json)
  1314. if (json.type === 2) {
  1315. //调用后台判断订单是否支付成功
  1316. let i = 0;
  1317. let timer = setInterval(() => {
  1318. postRequest('/api/receipt/check', {
  1319. receiptId: id
  1320. }, res => {
  1321. if (res.isSuccess || i === 4) {
  1322. clearInterval(timer);
  1323. success.call(this, res.isSuccess);
  1324. } else {
  1325. i++;
  1326. }
  1327. }, true)
  1328. }, 200);
  1329. // success.call(this);
  1330. return false;
  1331. }
  1332. uni.requestPayment({
  1333. timeStamp: json.timeStamp,
  1334. nonceStr: json.nonceStr,
  1335. package: json.packages,
  1336. signType: json.signType,
  1337. paySign: json.sign,
  1338. success: function() {
  1339. console.log('支付成功')
  1340. let i = 0;
  1341. let timer = setInterval(() => {
  1342. postRequest('/api/receipt/check', {
  1343. receiptId: id
  1344. }, res => {
  1345. console.log(i)
  1346. if (res.isSuccess || i === 5) {
  1347. clearInterval(timer);
  1348. success.call(this, res.isSuccess);
  1349. } else {
  1350. i++;
  1351. }
  1352. }, true)
  1353. }, 500);
  1354. },
  1355. fail: function(res) {
  1356. console.log('支付失败')
  1357. }
  1358. })
  1359. if (isShowLoading) {
  1360. uni.hideLoading();
  1361. isShowLoading = false;
  1362. }
  1363. })
  1364. }
  1365. const authSetting = (authority, success, error) => {
  1366. // console.log(authority,success)
  1367. // #ifndef H5
  1368. uni.getSetting({
  1369. success(res) {
  1370. if (res.authSetting[authority]) {
  1371. success.call(this);
  1372. return false;
  1373. }
  1374. uni.authorize({
  1375. scope: authority,
  1376. success() {
  1377. success.call(this);
  1378. },
  1379. fail: function(res) {
  1380. error.call(this);
  1381. }
  1382. })
  1383. }
  1384. })
  1385. // #endif
  1386. }
  1387. const getLocation = (suss) => {
  1388. // #ifdef MP-WEIXIN
  1389. authSetting('scope.userLocation', () => {
  1390. // load('定位中…');
  1391. uni.getLocation({
  1392. type: 'gcj02',
  1393. // isHighAccuracy: true,
  1394. success: function(res) {
  1395. // uni.hideLoading();
  1396. console.log(res, "789")
  1397. suss.call(this, res);
  1398. },
  1399. fail: function(res) {
  1400. console.log("调用失败", res)
  1401. suss.call(this, 2)
  1402. // uni.hideLoading();
  1403. }
  1404. })
  1405. }, () => {
  1406. // uni.hideLoading();
  1407. console.log("取消")
  1408. suss.call(this, 1)
  1409. });
  1410. // #endif
  1411. // #ifdef MP-ALIPAY
  1412. uni.getLocation({
  1413. type: 'gcj02',
  1414. // isHighAccuracy: true,
  1415. success: function(res) {
  1416. // uni.hideLoading();
  1417. console.log(res, "789")
  1418. suss.call(this, res);
  1419. },
  1420. fail: function(res) {
  1421. console.log("调用失败", res)
  1422. suss.call(this, 2)
  1423. // uni.hideLoading();
  1424. }
  1425. })
  1426. // #endif
  1427. // #ifdef H5
  1428. uni.getLocation({
  1429. type: 'gcj02',
  1430. isHighAccuracy: true,
  1431. success: function(res) {
  1432. // uni.hideLoading();
  1433. console.log(res)
  1434. suss.call(this, res, "789");
  1435. },
  1436. fail: function(res) {
  1437. console.log("调用失败", res)
  1438. suss.call(this, 2)
  1439. // uni.hideLoading();
  1440. }
  1441. })
  1442. // #endif
  1443. }
  1444. const scopeAddress = (success) => {
  1445. // #ifdef MP-WEIXIN
  1446. authSetting('scope.address', () => {
  1447. uni.chooseAddress({
  1448. success: function(res) {
  1449. success.call(this, res);
  1450. },
  1451. })
  1452. }, () => {
  1453. msg('未设置开放权限')
  1454. });
  1455. // #endif
  1456. // #ifdef MP-ALIPAY
  1457. uni.chooseAddress({
  1458. success: function(res) {
  1459. success.call(this, res);
  1460. },
  1461. })
  1462. // #endif
  1463. }
  1464. const isAuth = () => {
  1465. const user = getStorage('userInfo');
  1466. return user && user.id;
  1467. }
  1468. // 去掉字符串中的特殊字符和转义字符
  1469. const excludeSpecial = (s) => {
  1470. // 去掉转义字符
  1471. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  1472. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  1473. if (s != undefined || s != null) {
  1474. s = s.replace(pattern, "")
  1475. }
  1476. return s;
  1477. }
  1478. // 行为操作
  1479. const saveBehavior = (params, success) => {
  1480. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  1481. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1482. //上级浏览ID:parentId
  1483. //来源用户:pid
  1484. postRequest('/api/v3/save', params, data => {
  1485. success.call(this, data)
  1486. })
  1487. }
  1488. const getBehavior = (params, success) => {
  1489. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  1490. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1491. getRequest('/api/v3/get', params, data => {
  1492. success.call(this, data)
  1493. })
  1494. }
  1495. const getBehaviorCount = (params, success) => {
  1496. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认
  1497. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1498. getRequest('/api/v3/behavior/count', params, data => {
  1499. success.call(this, data)
  1500. })
  1501. }
  1502. const getBehaviorUpdate = (params, success) => {
  1503. //主键id:save接口返回的id
  1504. postRequest('/api/v3/update', params, data => {
  1505. success.call(this, data)
  1506. })
  1507. }
  1508. const isTabPage = (pageName, solution_name) => {
  1509. console.log('isTabPage', pageName)
  1510. // pageName 当前页面channel solution_name 线上配置name
  1511. let pass = false;
  1512. if (pageName) {
  1513. for (let i = 0; i < tab.length; i++) {
  1514. let it = tab[i];
  1515. if (solution_name && solution_name != ' ') {
  1516. if (it.name == solution_name) {
  1517. let tlist = it.list.map(iit => {
  1518. return iit.channel
  1519. })
  1520. if (tlist.indexOf(pageName) != -1) return true
  1521. }
  1522. } else {
  1523. if (it.name == 'default') {
  1524. let tlist = it.list.map(iit => {
  1525. return iit.channel
  1526. })
  1527. if (tlist.indexOf(pageName) != -1) return true
  1528. }
  1529. }
  1530. }
  1531. }
  1532. return pass
  1533. }
  1534. const toTabBar = (pageName, options = {}) => {
  1535. console.log('toTabBar', pageName)
  1536. // let url = '/pages/tabBar/index?pageChannel='+pageName
  1537. // if(Object.keys(options).length>0){
  1538. // for (let i in options) {
  1539. // url = url+'&'+i+'='+options[i]
  1540. // }
  1541. // }
  1542. options.pageChannel = pageName
  1543. console.log('toTabBar options', options)
  1544. setStorage('optionsPageDate', options)
  1545. uni.switchTab({
  1546. url: '/pages/tabBar/index'
  1547. })
  1548. }
  1549. /**
  1550. * 记录用户端口和来源
  1551. * @param port 用户端口(不需要传) 1.小程序(默认) 2.微信好友 3.微信群 4.企微好友 5.企微群 6.H5 7.公众号 8.导入 9.APP
  1552. * @param source 来源 1 小程序搜索 2 活动报名 3 名片分享 4商品分享 5店铺分享 6文章分享 7小程序分享 8素材分享 9公众号分享 10公众号搜索 11企微活码 12微信活码 13手工录入 14 5g消息
  1553. * @param uid 用户id
  1554. */
  1555. const savePortAndSource = ({
  1556. port = 1,
  1557. source = '',
  1558. uid = ''
  1559. } = {}) => {
  1560. // 获取用户端口
  1561. portObj = getUserPort()
  1562. let dataP = {
  1563. portType: portObj.port,
  1564. resourceType: source,
  1565. uid: uid
  1566. };
  1567. // 如果是从好友或群聊端口进入且没有来源则算作 通过小程序分享进入
  1568. if ((dataP.portType == 2 || dataP.portType == 3 || dataP.portType == 4 || dataP.portType == 5) && !
  1569. source) {
  1570. dataP.resourceType = 7
  1571. }
  1572. // 端口有来源则展示端口来源
  1573. if (portObj.resourceType) {
  1574. dataP.resourceType = portObj.resourceType
  1575. }
  1576. // 默认为主动进入小程序用户,H5 APP暂时未开放,如需统计主动进入或分享H5 APP等数据需要再讨论用户来源类型
  1577. if (!dataP.resourceType) dataP.resourceType = 1
  1578. console.log('调用用户端口和来源保存接口:', dataP)
  1579. postRequest('/api/v3/application/behavior/saveUserResourcePort', dataP, data => {
  1580. });
  1581. }
  1582. const getWebchatAppId = () => {
  1583. // #ifdef H5
  1584. if (utils.isWeixin()) {
  1585. getRequest(api.webchat_appId, {}, data => {
  1586. if (data) {
  1587. setStorage('webchatAppId', data);
  1588. }
  1589. });
  1590. }
  1591. // #endif
  1592. }
  1593. const getwxConfig = (suc) => {
  1594. getRequest('/api/config', {}, data => {
  1595. setStorage('configRes', JSON.stringify(data));
  1596. getApp().globalData.upColor(true)
  1597. suc.call(this, data);
  1598. });
  1599. }
  1600. // 获取用户端口
  1601. const getUserPort = () => {
  1602. const sinfo = uni.getSystemInfoSync();
  1603. let port = 1
  1604. let resourceType = null
  1605. // #ifdef H5
  1606. console.log("当前端口: H5端")
  1607. port = 6
  1608. // #endif
  1609. // #ifdef APP
  1610. console.log("当前端口: APP端")
  1611. port = 9
  1612. // #endif
  1613. // #ifdef MP-WEIXIN
  1614. console.log("当前端口: 小程序端")
  1615. port = 1
  1616. let params = uni.getEnterOptionsSync()
  1617. console.log("小程序端参数: ", params)
  1618. switch (params.scene) {
  1619. case 1007: { //单人聊天会话中的小程序消息卡片
  1620. port = 2
  1621. if (sinfo.environment == 'wxwork') {
  1622. port = 4
  1623. }
  1624. }
  1625. break;
  1626. case 1008: { //群聊会话中的小程序消息卡片
  1627. port = 3
  1628. console.log("小程序群场景值1008")
  1629. if (sinfo.environment == 'wxwork') {
  1630. console.log("小程序群场景值1008 企微环境")
  1631. port = 5
  1632. }
  1633. try {
  1634. uni.getGroupEnterInfo({
  1635. success: (res) => {
  1636. console.log("getGroupEnterInfo 小程序群聊id:", res)
  1637. // if (res.encryptedData && res.iv) {
  1638. // app.globalData.getCheckSessoin(json => {
  1639. // let _params = {
  1640. // sessionKey: json.sessionKey,
  1641. // openId: json.openid,
  1642. // iv: res.iv,
  1643. // encryptedData: res.encryptedData
  1644. // };
  1645. // postRequest('', _params, json => {
  1646. // });
  1647. // });
  1648. // }
  1649. },
  1650. fail: (error) => {
  1651. console.log("小程序群聊id error:", error)
  1652. },
  1653. })
  1654. } catch (e) {
  1655. //TODO handle the exception
  1656. }
  1657. }
  1658. break;
  1659. case 1044: { //带 shareTicket 的小程序消息卡片(群聊)
  1660. port = 3
  1661. if (sinfo.environment == 'wxwork') {
  1662. port = 5
  1663. }
  1664. console.log("小程序群聊id:", params.shareTicket)
  1665. try {
  1666. uni.getGroupEnterInfo({
  1667. success: (res) => {
  1668. console.log("小程序群聊id:", res)
  1669. }
  1670. })
  1671. } catch (e) {
  1672. //TODO handle the exception
  1673. }
  1674. }
  1675. break;
  1676. case 1020: { //公众号 profile 页相关小程序列表(已废弃)
  1677. port = 7
  1678. }
  1679. break;
  1680. case 1035: { //公众号自定义菜单
  1681. port = 7
  1682. }
  1683. break;
  1684. case 1043: { //公众号模板消息
  1685. port = 7
  1686. }
  1687. break;
  1688. case 1074: { //公众号会话下发的小程序消息卡片
  1689. port = 7
  1690. }
  1691. break;
  1692. case 1082: { //公众号会话下发的文字链
  1693. port = 7
  1694. }
  1695. break;
  1696. case 1091: { //公众号文章商品卡片
  1697. port = 7
  1698. }
  1699. break;
  1700. case 1065: { //URL scheme 用户5g消息进入小程序
  1701. port = 1
  1702. resourceType = 14
  1703. }
  1704. break;
  1705. default:
  1706. break;
  1707. }
  1708. // #endif
  1709. // 目前判断从企微环境打开的都算企微好友,无发获取到是从群聊还是好友聊天中打开的程序
  1710. if (sinfo.environment == 'wxwork') {
  1711. port = 4
  1712. }
  1713. console.log("进入端口类型: ", port)
  1714. return {
  1715. port: port,
  1716. resourceType: resourceType
  1717. }
  1718. }
  1719. /**
  1720. * text:展示导航名
  1721. * iconPath:未点击显示图标
  1722. * selectedIconPath:选中图标
  1723. * pagePath:页面路径
  1724. * channel:匹配名
  1725. * click:点击事件
  1726. * index:排序
  1727. * show:是否显示
  1728. */
  1729. let config = getStorage('configRes') ? JSON.parse(getStorage('configRes')) : {};
  1730. var tab = [{
  1731. name: 'default',
  1732. list: [{
  1733. text: "首页",
  1734. iconPath: "/static/pages/images/home.png",
  1735. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1736. pagePath: 'pages/index/index',
  1737. channel: 'home',
  1738. click: 'goHome',
  1739. index: 1,
  1740. show: true
  1741. },
  1742. {
  1743. text: "服务项目",
  1744. iconPath: '/static/pages/images/service.png',
  1745. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'service_h.png',
  1746. pagePath: 'pages/service/index',
  1747. click: 'goService',
  1748. channel: 'service',
  1749. index: 2,
  1750. show: true
  1751. },
  1752. {
  1753. text: "我的",
  1754. iconPath: '/static/pages/images/user.png',
  1755. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1756. pagePath: 'pages/user/user',
  1757. click: 'goUser',
  1758. channel: 'user',
  1759. index: 3,
  1760. show: true
  1761. }
  1762. ]
  1763. },
  1764. {
  1765. name: 'taoyanxi',
  1766. list: [{
  1767. text: "首页",
  1768. iconPath: "/static/pages/images/home.png",
  1769. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1770. iconPathOn: "home.png",
  1771. selectedIconPathOn: 'home_h.png',
  1772. pagePath: 'pages/index/index',
  1773. channel: 'home',
  1774. click: 'goHome',
  1775. index: 1,
  1776. show: true
  1777. },
  1778. {
  1779. text: "产品",
  1780. iconPath: '/static/pages/images/service.png',
  1781. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'service_h.png',
  1782. iconPathOn: "service.png",
  1783. selectedIconPathOn: 'service_h.png',
  1784. pagePath: 'pages/service/index',
  1785. click: 'goService',
  1786. channel: 'service',
  1787. index: 2,
  1788. show: true
  1789. },
  1790. {
  1791. text: "我的",
  1792. iconPath: '/static/pages/images/user.png',
  1793. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1794. iconPathOn: "user.png",
  1795. selectedIconPathOn: 'user_h.png',
  1796. pagePath: 'pages/user/user',
  1797. click: 'goUser',
  1798. channel: 'user',
  1799. index: 3,
  1800. show: true
  1801. }
  1802. ]
  1803. },
  1804. {
  1805. name: 'yaofang',
  1806. list: [{
  1807. text: "首页",
  1808. iconPath: "/static/pages/images/home.png",
  1809. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1810. iconPathOn: "home.png",
  1811. selectedIconPathOn: 'home_h.png',
  1812. pagePath: 'home/index/index_yaofang',
  1813. channel: 'home',
  1814. click: 'goHome',
  1815. index: 1,
  1816. show: true
  1817. },
  1818. {
  1819. text: "分类",
  1820. iconPath: '/static/pages/images/sort.png',
  1821. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'sort_h.png',
  1822. iconPathOn: "sort.png",
  1823. selectedIconPathOn: 'sort_h.png',
  1824. pagePath: 'pages/service/index',
  1825. click: 'goService',
  1826. channel: 'service',
  1827. index: 2,
  1828. show: true
  1829. },
  1830. {
  1831. text: "购物车",
  1832. iconPath: '/static/pages/images/cart.png',
  1833. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'cart_h.png',
  1834. iconPathOn: "cart.png",
  1835. selectedIconPathOn: 'cart_h.png',
  1836. pagePath: 'pages/cart/cart?isSwitchTab=true',
  1837. click: 'goCart',
  1838. channel: 'cart',
  1839. index: 3,
  1840. show: true
  1841. },
  1842. {
  1843. text: "我的",
  1844. iconPath: '/static/pages/images/user.png',
  1845. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1846. iconPathOn: "user.png",
  1847. selectedIconPathOn: 'user_h.png',
  1848. pagePath: 'pages/user/user',
  1849. click: 'goUser',
  1850. channel: 'user',
  1851. index: 4,
  1852. show: true
  1853. }
  1854. ]
  1855. }
  1856. ]
  1857. module.exports = {
  1858. setStorage: setStorage,
  1859. getStorage: getStorage,
  1860. postRequest: postRequest,
  1861. postRequestLoding: postRequestLoding,
  1862. getRequest: getRequest,
  1863. putRequest: putRequest,
  1864. putRequestJson: putRequestJson,
  1865. getRequestKDX: getRequestKDX,
  1866. postRequestKDX: postRequestKDX,
  1867. msg: msg,
  1868. g: g,
  1869. p: p,
  1870. env: env,
  1871. send: send,
  1872. redirectIndex: redirectIndex,
  1873. saveImage: saveImage,
  1874. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1875. uploadFile: uploadFile,
  1876. msgConfirm: msgConfirm,
  1877. getConfig: getConfig,
  1878. load: load,
  1879. isLogin: isLogin,
  1880. isLogins: isLogins,
  1881. isAuthFn:isAuthFn,
  1882. payOrder: payOrder,
  1883. payOrders: payOrders,
  1884. receipt: receipt,
  1885. getLocation: getLocation,
  1886. scopeAddress: scopeAddress,
  1887. isAuth: isAuth,
  1888. alertMsg: alertMsg,
  1889. getToken: getToken,
  1890. loadIng: loadIng,
  1891. removeStorage: removeStorage,
  1892. public: publics,
  1893. header: header,
  1894. // gettmplIds: gettmplIds,
  1895. excludeSpecial: excludeSpecial,
  1896. tab: tab,
  1897. msgConfirmText: msgConfirmText,
  1898. silenceLogin: silenceLogin,
  1899. getWXUserProfile: getWXUserProfile,
  1900. updataWxInfos: updataWxInfos,
  1901. saveBehavior: saveBehavior,
  1902. getBehavior: getBehavior,
  1903. getBehaviorCount: getBehaviorCount,
  1904. getBehaviorUpdate: getBehaviorUpdate,
  1905. isTabPage: isTabPage,
  1906. toTabBar: toTabBar,
  1907. updataWxInfos: updataWxInfos,
  1908. initIMChat: initIMChat,
  1909. getIMToken: getIMToken,
  1910. jumpConversation: jumpConversation,
  1911. jumpSingleChatRoom: jumpSingleChatRoom,
  1912. onNetworkStatusChangeIM: onNetworkStatusChangeIM,
  1913. payAppOrder: payAppOrder,
  1914. savePortAndSource: savePortAndSource,
  1915. getUserPort: getUserPort,
  1916. getWebchatAppId: getWebchatAppId,
  1917. checkPaySuccess: paySuccess,
  1918. getwxConfig:getwxConfig
  1919. }