Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShopPublicHandle.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-1-7
  12. */
  13. namespace app\common\model;
  14. use think\Db;
  15. use think\Cache;
  16. use think\Config;
  17. use think\Cookie;
  18. /**
  19. * 商城公共处理模型
  20. */
  21. load_trait('controller/Jump');
  22. class ShopPublicHandle
  23. {
  24. use \traits\controller\Jump;
  25. // 微信授权 cookie 操作
  26. public function weChatauthorizeCookie($users_id = 0, $type = 'get', $save = [])
  27. {
  28. $cookieID = $users_id . '_weChatAuthorize';
  29. if ('get' == $type) {
  30. $open_id = Cookie::get($cookieID);
  31. return !empty($open_id) ? $open_id : '';
  32. } else if ('set' == $type) {
  33. Cookie::set($cookieID, $save['openid'], ['expire' => $save['expire']]);
  34. } else if ('del' == $type) {
  35. Cookie::delete($cookieID);
  36. }
  37. }
  38. // 获取订单商品规格列表(购买时的商品规格)
  39. public function getOrderGoodsSpecList($goods = [])
  40. {
  41. // 规格处理
  42. $goodsSpecList = [];
  43. $goodsSpec = !empty($goods['data']) ? unserialize($goods['data']) : [];
  44. if (!empty($goodsSpec['spec_value'])) {
  45. $specValueArr = explode('<br/>', htmlspecialchars_decode($goodsSpec['spec_value']));
  46. foreach ($specValueArr as $value_10000) {
  47. $arr_10000 = !empty($value_10000) ? explode(':', $value_10000) : [];
  48. if (!empty($arr_10000[0]) && trim($arr_10000[0])) {
  49. $goodsSpecList[] = [
  50. 'name' => !empty($arr_10000[0]) ? trim($arr_10000[0]) : '',
  51. 'value' => !empty($arr_10000[1]) ? trim($arr_10000[1]) : '',
  52. ];
  53. }
  54. }
  55. }
  56. // 返回规格列表
  57. return $goodsSpecList;
  58. }
  59. // 获取商品规格列表(查询数据库)
  60. public function getGoodsSpecList($goods = '')
  61. {
  62. $goodsSpecList = [];
  63. if (!empty($goods['spec_value_id'])) {
  64. $spec_value_id = explode('_', $goods['spec_value_id']);
  65. if (!empty($spec_value_id)) {
  66. $where = [
  67. 'aid' => $goods['aid'],
  68. 'lang' => get_home_lang(),
  69. 'spec_value_id' => ['IN', $spec_value_id]
  70. ];
  71. $productSpecData = Db::name("product_spec_data")->where($where)->field('spec_name, spec_value')->select();
  72. foreach ($productSpecData as $value_10001) {
  73. $goodsSpecList[] = [
  74. 'name' => !empty($value_10001['spec_name']) ? trim($value_10001['spec_name']) : '',
  75. 'value' => !empty($value_10001['spec_value']) ? trim($value_10001['spec_value']) : '',
  76. ];
  77. }
  78. }
  79. }
  80. // 返回规格列表
  81. return $goodsSpecList;
  82. }
  83. // 获取指定小程序的配置信息
  84. public function getSpecifyAppletsConfig()
  85. {
  86. // 小程序ID及小程序标识
  87. $applets_id = input('param.applets_id/d', 0);
  88. $applets_mark = input('param.provider/s', '');
  89. $result = [];
  90. // 获取插件中创建的小程序配置
  91. if ($this->getWeappInfo('Suibian') && !empty($applets_id) && !empty($applets_mark)) {
  92. $result = model('WeappAppletsConfigList')->getAppletsConfigDetails($applets_id, $applets_mark);
  93. $result = !empty($result['config']) ? $result['config']['applets_config'] : [];
  94. }
  95. // 获取系统中创建的小程序配置
  96. else if (!empty($applets_mark)) {
  97. $result = tpSetting("OpenMinicode.conf_" . trim($applets_mark));
  98. $result = !empty($result) ? json_decode($result, true) : [];
  99. }
  100. return $result;
  101. }
  102. // 获取微信小程序支付信息
  103. public function getWechatAppletsPay($users_id = 0, $code = '', $prices = 0, $type = 2, $config = [])
  104. {
  105. // 查询会员微信小程序 openid
  106. $openid = Db::name('wx_users')->where('users_id', $users_id)->getField('openid');
  107. // 调用user模块支付API模型
  108. $payApiModel = new \app\user\model\PayApi();
  109. // 返回微信小程序支付信息
  110. return $payApiModel->getWechatAppletsPay($openid, $code, $prices, $type, $config);
  111. }
  112. // 获取微信支付结果并执行支付成功业务逻辑
  113. public function getWeChatPayResult($users_id, $orderData = [], $type = 2)
  114. {
  115. // 查询会员微信小程序 openid
  116. $openid = Db::name('wx_users')->where('users_id', $users_id)->getField('openid');
  117. // 调用user模块支付API模型
  118. $payApiModel = new \app\user\model\PayApi();
  119. // 返回微信小程序支付信息
  120. $result = $payApiModel->getWeChatPayResult($openid, $orderData['unified_number']);
  121. if (!empty($result['return_code'])) {
  122. // 如果存在错误则直接提示
  123. if ('FAIL' == $result['return_code'] && !empty($result['return_msg'])) $this->error($result['return_msg']);
  124. // 查询成功后续处理
  125. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  126. if ($result['trade_state'] == 'SUCCESS' && !empty($result['transaction_id'])) {
  127. $payApiLogic = new \app\user\logic\PayApiLogic($users_id);
  128. $payApiLogic->OrderProcessing($orderData, $orderData, $result);
  129. } else if ($result['trade_state'] == 'NOTPAY') {
  130. $this->error('订单尚未完成支付');
  131. }
  132. }
  133. }
  134. // 未知异常则返回提示
  135. $this->error('微信接口异常,如已真实完成支付请联系客服查询微信支付');
  136. }
  137. // 商品购买确认页 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  138. public function goodsBuyPagePublicHandle($resultData = [])
  139. {
  140. // 其他特殊处理和兼容
  141. // ...........
  142. // 返回数据
  143. return $resultData;
  144. }
  145. // 订单提交处理 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  146. public function orderSubmitPublicHandle($orderData = [], $usersConfig = [], $usersID = 0, $post = [], $list = [])
  147. {
  148. // 如果后台【商城中心】-【商城配置】-【订单设置】-收货后可维权时间设置为0,则表示订单不允许申请维权,反之允许申请维权
  149. $orderData['allow_service'] = empty($usersConfig['order_right_protect_time']) ? 1 : 0;
  150. // 获取消费获得积分数据
  151. $orderData['obtain_scores'] = getConsumObtainScores($orderData, $usersConfig, true);
  152. $orderData['is_obtain_scores'] = !empty($orderData['obtain_scores']) ? 0 : 1;
  153. // 会员信息
  154. $userInfo = GetUsersLatestData($usersID);
  155. // 如果安装了分销插件则执行
  156. if (is_dir('./weapp/DealerPlugin/')) {
  157. // 开启分销插件则执行
  158. $weappInfo = model('Weapp')->getWeappList('DealerPlugin');
  159. if (!empty($weappInfo['status']) && 1 === intval($weappInfo['status'])) {
  160. // 调用分销逻辑层方法
  161. $dealerCommonLogic = new \weapp\DealerPlugin\logic\DealerCommonLogic;
  162. $orderData = $dealerCommonLogic->dealerOrderHandle($orderData, $userInfo);
  163. }
  164. }
  165. // 返回数据
  166. return $orderData;
  167. }
  168. // 订单创建后续处理 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  169. public function orderCreatePublicHandle($orderData = [], $usersConfig = [], $usersID = 0, $post = [], $list = [])
  170. {
  171. // 会员信息
  172. $userInfo = GetUsersLatestData($usersID);
  173. // 获取核销插件数据
  174. $weappInfo = $this->getWeappVerifyInfo();
  175. // 开启核销插件则执行
  176. if (!empty($weappInfo['status']) && 1 === intval($weappInfo['status'])) {
  177. // 调用核销逻辑层方法
  178. $verifyLogic = new \weapp\Verify\logic\VerifyLogic;
  179. $orderData = $verifyLogic->verifyOrderCreateHandle($orderData, $userInfo, $post, $weappInfo);
  180. }
  181. // 返回数据
  182. return $orderData;
  183. }
  184. // 订单支付完成处理 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  185. public function orderPayCompletePublicHandle($post = [], $userInfo = [], $notify = false, $shopOrder = [], $resultData = [], $goodsList = [])
  186. {
  187. // 其他特殊处理和兼容
  188. // ...........
  189. }
  190. // 获取售后订单是否是归属商城订单的最后一个售后订单,1是,0否
  191. public function isLastOneServiceOrder($users_id = 0, $order_id = 0)
  192. {
  193. // 查询所属订单下有多少个商品
  194. $where = [
  195. 'users_id' => intval($users_id),
  196. 'order_id' => intval($order_id),
  197. ];
  198. $goodsCount = Db::name('shop_order_details')->where($where)->count();
  199. // 查询所属订单下有多少个已完成维权的商品
  200. $where = [
  201. 'status' => ['IN', [6, 7]],
  202. 'users_id' => intval($users_id),
  203. 'order_id' => intval($order_id),
  204. ];
  205. $applyCount = Db::name('shop_order_service')->where($where)->count();
  206. // 计算维权是否为本次商品订单中的最后一个维权商品
  207. return 1 === intval(intval($goodsCount) - intval($applyCount)) ? 1 : 0;
  208. }
  209. // H5终端检测处理
  210. public function detectH5Terminal($terminal = 'h5')
  211. {
  212. return !empty($terminal) && 'h5' === strval($terminal) ? true : false;
  213. }
  214. // 查询收货地址和运费计算
  215. public function getSystemAddress($users_id = 0, $systemShopConfig = [], $param = [])
  216. {
  217. // 查询用户第一个收货地址,若有默认地址则使用默认收货地址
  218. $where = [
  219. 'users_id' => intval($users_id),
  220. ];
  221. if (!empty($param['addr_id'])) $where['addr_id'] = intval($param['addr_id']);
  222. $address = Db::name('shop_address')->where($where)->order('is_default desc')->find();
  223. if (!empty($address)) {
  224. // 收货地址地区名称转换
  225. $address['region']['province'] = get_province_name($address['province']);
  226. $address['region']['city'] = get_city_name($address['city']);
  227. $address['region']['district'] = get_area_name($address['district']);
  228. $address['region']['detail'] = $address['address'];
  229. $address['address_all'] = $address['region']['province'] . $address['region']['city'] . $address['region']['district'] . $address['region']['detail'];
  230. // 查询运费
  231. if (!empty($systemShopConfig['shop_open_shipping'])) {
  232. $where = [
  233. 'province_id' => intval($address['province']),
  234. ];
  235. $template_money = Db::name('shop_shipping_template')->where($where)->getField('template_money');
  236. if (0 == unifyPriceHandle($template_money)) {
  237. $where = [
  238. 'province_id' => 100000,
  239. ];
  240. $template_money = Db::name('shop_shipping_template')->where($where)->getField('template_money');
  241. }
  242. $address['template_money'] = unifyPriceHandle($template_money);
  243. } else {
  244. $address['template_money'] = 0;
  245. }
  246. }
  247. return $address;
  248. }
  249. // 查询微信支付是否开启
  250. public function getWeChatPayOpen($isApplets = false)
  251. {
  252. $result = true;
  253. if (empty($isApplets)) {
  254. $where = [
  255. 'pay_id' => 1,
  256. 'pay_mark' => 'wechat',
  257. ];
  258. $weChatConfig = Db::name('pay_api_config')->where($where)->getField('pay_info');
  259. $weChatConfig = !empty($weChatConfig) ? unserialize($weChatConfig) : [];
  260. if (empty($weChatConfig) || 1 === intval($weChatConfig['is_open_wechat']) || empty($weChatConfig['appid']) || empty($weChatConfig['mchid']) || empty($weChatConfig['key'])) $result = false;
  261. } else {
  262. $miniproWeixin = json_decode(tpSetting("OpenMinicode.conf_weixin"), true);
  263. if (empty($miniproWeixin) || 1 === intval($miniproWeixin['appid']) || empty($miniproWeixin['mchid']) || empty($miniproWeixin['apikey'])) $result = false;
  264. }
  265. return $result;
  266. }
  267. // 查询余额支付是否开启
  268. public function getBalancePayOpen()
  269. {
  270. $pay_balance_open = getUsersConfigData('pay.pay_balance_open');
  271. return !empty($pay_balance_open) ? true : false;
  272. }
  273. // 获取支付类型的隐藏域
  274. public function getPayApiHidden($param = [])
  275. {
  276. $where = [
  277. 'status' => 1
  278. ];
  279. // 手机端微信、小程序不查询支付宝配置
  280. if ((isMobile() && isWeixin()) || isWeixinApplets()) $where['pay_mark'] = ['NEQ', 'alipay'];
  281. // 查询支付配置
  282. $payApiList = Db::name('pay_api_config')->where($where)->select();
  283. // 默认选中支付方式,3:货到付款,2:余额支付,1:在线支付,0:未开启支付方式
  284. $usePayType = 0;
  285. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="">';
  286. // 封装订单支付方式隐藏域
  287. if (empty($param['shop_open_offline']) && empty($param['PromType'])) {
  288. $usePayType = 3;
  289. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="1"><input type="hidden" name="payment_type" id="payment_type" value="hdfk_payOnDelivery">';
  290. }
  291. // 在线支付判断
  292. if (!empty($payApiList)) {
  293. foreach ($payApiList as $key => $value) {
  294. $PayInfo = unserialize($value['pay_info']);
  295. if ('wechat' == $value['pay_mark']) {
  296. // 微信判断
  297. if ((isset($PayInfo['is_open_wechat']) && 0 == $PayInfo['is_open_wechat']) || false === $this->findHupijiaoPay('wechat')) {
  298. $usePayType = 1;
  299. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="zxzf_wechat">';
  300. break;
  301. }
  302. } else if ('alipay' == $value['pay_mark']) {
  303. // 支付宝判断
  304. if ((isset($PayInfo['is_open_alipay']) && 0 == $PayInfo['is_open_alipay']) || false === $this->findHupijiaoPay('alipay')) {
  305. $usePayType = 1;
  306. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="zxzf_alipay">';
  307. break;
  308. }
  309. } else if (0 == $value['system_built']) {
  310. // 第三方支付判断
  311. if (isset($PayInfo['is_open_pay']) && 0 == $PayInfo['is_open_pay']) {
  312. $usePayType = 1;
  313. if (!empty($PayInfo['wechat_appid']) && !empty($PayInfo['wechat_appsecret'])) {
  314. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="zxzf_wechat">';
  315. break;
  316. } else if (!empty($PayInfo['alipay_appid']) && !empty($PayInfo['alipay_appsecret'])) {
  317. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="zxzf_alipay">';
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. // 余额支付判断
  325. if (in_array($usePayType, [0, 3]) && 1 === intval($param['pay_balance_open'])) {
  326. $usePayType = 2;
  327. $payTypeHidden = '<input type="hidden" name="payment_method" id="payment_method" value="0"><input type="hidden" name="payment_type" id="payment_type" value="yezf_balance">';
  328. }
  329. return [
  330. 'usePayType' => $usePayType,
  331. 'payTypeHidden' => $payTypeHidden,
  332. ];
  333. }
  334. //查询虎皮椒支付有没有配置相应的(微信or支付宝)支付
  335. public function findHupijiaoPay($type = '')
  336. {
  337. $hupijiaoInfo = Db::name('weapp')->where(['code'=>'Hupijiaopay','status'=>1])->find();
  338. $hupijiaoPay = Db::name('pay_api_config')->where(['pay_mark'=>'Hupijiaopay'])->find();
  339. if (empty($hupijiaoPay) || empty($hupijiaoInfo)) return true;
  340. if (empty($hupijiaoPay['pay_info'])) return true;
  341. $payInfo = unserialize($hupijiaoPay['pay_info']);
  342. if (empty($payInfo)) return true;
  343. if (!isset($payInfo['is_open_pay']) || $payInfo['is_open_pay'] == 1) return true;
  344. $type .= '_appid';
  345. if (!isset($payInfo[$type]) || empty($payInfo[$type])) return true;
  346. return false;
  347. }
  348. // 获取核销插件数据
  349. public function getWeappVerifyInfo()
  350. {
  351. $result = [];
  352. // 如果安装了核销插件则执行
  353. if (is_dir('./weapp/Verify/')) {
  354. // 核销插件数据信息
  355. $result = model('Weapp')->getWeappList('Verify');
  356. }
  357. // 返回数据
  358. return $result;
  359. }
  360. // 获取积分商城插件数据
  361. public function getWeappPointsShop()
  362. {
  363. $result = [];
  364. // 如果安装了积分商城插件则执行
  365. if (is_dir('./weapp/PointsShop/')) {
  366. // 积分商城插件数据信息
  367. $result = model('Weapp')->getWeappList('PointsShop');
  368. }
  369. // 返回数据
  370. return $result;
  371. }
  372. //判断是否安装某插件 传插件标识即可用
  373. public function getWeappInfo($code = '')
  374. {
  375. $result = [];
  376. // 如果安装了某个插件则执行
  377. if (is_dir("./weapp/{$code}/")) {
  378. // 某插件数据信息
  379. $result = model('Weapp')->getWeappList($code);
  380. }
  381. // 返回数据
  382. return $result;
  383. }
  384. // 系统商品操作时,积分商品的被动处理
  385. public function pointsGoodsPassiveHandle($aid = [])
  386. {
  387. $weappInfo = $this->getWeappPointsShop();
  388. if (!empty($weappInfo)) {
  389. $pointsShopLogic = new \weapp\PointsShop\logic\PointsShopLogic();
  390. $pointsShopLogic->pointsGoodsPassiveHandle($aid);
  391. }
  392. }
  393. // 执行每日签到赠送积分
  394. public function executeUsersDailyCheckIns($users_id = 0)
  395. {
  396. // 获取系统积分规则
  397. $systemPointsRules = getUsersConfigData('score');
  398. if (empty($systemPointsRules) || !isset($systemPointsRules['score_signin_status']) || $systemPointsRules['score_signin_status'] != 1) {
  399. return [
  400. 'code' => 0,
  401. 'msg' => '签到送积分功能已关闭',
  402. ];
  403. }
  404. // 查询今日签到信息
  405. $times = time();
  406. $today_0 = mktime(0, 0, 0, date("m", $times), date("d", $times), date("Y", $times));
  407. $today_1 = mktime(23, 59, 59, date("m", $times), date("d", $times), date("Y", $times));
  408. $where = [
  409. 'users_id' => intval($users_id),
  410. 'add_time' => ['BETWEEN', [$today_0, $today_1]]
  411. ];
  412. $result = Db::name('users_signin')->where($where)->count();
  413. // 未签到则执行签到赠送积分
  414. if (empty($result)) {
  415. // 添加签到记录
  416. $insert = [
  417. 'users_id' => intval($users_id),
  418. 'add_time' => $times,
  419. ];
  420. $insertID = Db::name('users_signin')->insertGetId($insert);
  421. if (!empty($insertID)) {
  422. // 更新会员积分
  423. $where = [
  424. 'users_id' => intval($users_id),
  425. ];
  426. $signinScore = $systemPointsRules['score_signin_score'] ? intval($systemPointsRules['score_signin_score']) : 0;
  427. Db::name('users')->where($where)->setInc('scores', $signinScore);
  428. $usersScores = Db::name('users')->where($where)->value('scores');
  429. // 添加会员积分记录
  430. $insert = [
  431. 'type' => 5,
  432. 'users_id' => intval($users_id),
  433. 'score' => '+' . intval($signinScore),
  434. 'devote' => intval($signinScore),
  435. 'info' => '每日签到',
  436. 'add_time' => $times,
  437. 'update_time' => $times,
  438. 'current_score' => intval($usersScores),
  439. 'remark' => '签到赠送积分',
  440. ];
  441. Db::name('users_score')->insert($insert);
  442. // 返回成功提示
  443. return [
  444. 'code' => 1,
  445. 'msg' => '签到成功',
  446. 'scores' => $usersScores,
  447. 'checkInsPoints' => $signinScore,
  448. ];
  449. }
  450. // 签到异常,刷新重试
  451. return [
  452. 'code' => 0,
  453. 'msg' => '签到异常,刷新重试',
  454. ];
  455. }
  456. // 今日已签到过
  457. return [
  458. 'code' => 0,
  459. 'msg' => '今日已签到过',
  460. ];
  461. }
  462. // 获取会员积分明细数据
  463. public function getBaseUsersPointsDetails($users_id = 0, $param = [])
  464. {
  465. // 会员积分查询
  466. $where = [
  467. 'users_id' => intval($users_id)
  468. ];
  469. if (!empty($param['navID']) && 1 === intval($param['navID'])) {
  470. $where[] = Db::raw('score > 0');
  471. } else if (!empty($param['navID']) && 2 === intval($param['navID'])) {
  472. $where[] = Db::raw('score < 0');
  473. }
  474. $pagesize = config('paginate.list_rows') ? config('paginate.list_rows') : 15;
  475. $result = Db::name('users_score')->where($where)->order('add_time desc')->paginate($pagesize, false, ['query' => request()->param()]);
  476. !empty($result) && $result = $result->toArray();
  477. // 处理积分数据
  478. foreach ($result['data'] as $key => $value) {
  479. $value['type_title'] = '';
  480. if (1 === intval($value['type'])) {
  481. $value['type_title'] = '提问';
  482. } else if (2 === intval($value['type'])) {
  483. $value['type_title'] = '回答';
  484. } else if (3 === intval($value['type'])) {
  485. $value['type_title'] = '最佳答案';
  486. } else if (4 === intval($value['type'])) {
  487. $value['type_title'] = '悬赏退回';
  488. } else if (5 === intval($value['type'])) {
  489. $value['type_title'] = '每日签到';
  490. } else if (6 === intval($value['type'])) {
  491. $value['type_title'] = '管理员编辑';
  492. } else if (7 === intval($value['type']) && intval($value['score']) < 0) {
  493. $value['type_title'] = '问题悬赏';
  494. } else if (7 === intval($value['type']) && intval($value['score']) > 0) {
  495. $value['type_title'] = '问题获得悬赏';
  496. } else if (8 === intval($value['type'])) {
  497. $value['type_title'] = '消费赠送积分';
  498. } else if (9 === intval($value['type']) && intval($value['score']) < 0) {
  499. $value['type_title'] = '积分商城';
  500. } else if (9 === intval($value['type']) && intval($value['score']) > 0) {
  501. $value['type_title'] = '积分商城取消';
  502. } else if (10 === intval($value['type']) && intval($value['score']) > 0) {
  503. $value['type_title'] = '登录赠送积分';
  504. } else if (11 === intval($value['type'])) {
  505. $value['type_title'] = '积分商城订单支付';
  506. }
  507. $value['add_time'] = date('Y/m/d H:i:s', $value['add_time']);
  508. $result['data'][$key] = $value;
  509. }
  510. $navList = [
  511. ['id' => 0, 'name' => '全部记录'],
  512. ['id' => 1, 'name' => '积分获得'],
  513. ['id' => 2, 'name' => '积分消费']
  514. ];
  515. return [
  516. 'points' => $result,
  517. 'navList' => $navList,
  518. ];
  519. }
  520. // 处理会员折扣价返回
  521. public function handleUsersDiscountPrice($aid = 0, $level_id = 0)
  522. {
  523. // 会员折扣价
  524. $usersDiscountPrice = 0;
  525. // 查询会员折扣价列表
  526. $discountList = Db::name('product_users_discount')->where('aid', $aid)->getAllWithIndex('level_id');
  527. // 检测是否存在会员折扣价,没有则直接返回原数据
  528. if (!empty($discountList[$level_id])) {
  529. $usersDiscountPrice = !empty($discountList[$level_id]['users_discount_price']) ? floatval($discountList[$level_id]['users_discount_price']) : 0;
  530. }
  531. // 返回数据
  532. return $usersDiscountPrice;
  533. }
  534. // 获取会员折扣价格模板
  535. public function getUsersDiscountPriceTpl($aid = 0, $usersPrice = 0)
  536. {
  537. // 查询会员级别列表
  538. $usersLevelList = model('UsersLevel')->getList();
  539. if (empty($usersLevelList)) return ['code' => 0, 'data' => '请先在[会员中心]-[会员级别]中添加会员级别!'];
  540. // 如果存在产品ID则查询是否已指定会员级别
  541. $discountList = !empty($aid) ? Db::name('product_users_discount')->where('aid', $aid)->getAllWithIndex('level_id') : [];
  542. // 生成模板返回
  543. $resultTpl = $this->createUsersDiscountPriceTpl($usersLevelList, $discountList, $usersPrice);
  544. return ['code' => 1, 'data' => $resultTpl];
  545. }
  546. // 获取会员折扣价格模板
  547. public function saveUsersDiscountPriceList($usersDiscount = [], $aid = 0)
  548. {
  549. // 处理折扣价列表
  550. $insertAll = [];
  551. $times = getTime();
  552. $productUsersDiscount = Db::name('product_users_discount');
  553. $discount_ids = !empty($usersDiscount['id']) ? $usersDiscount['id'] : [];
  554. $discount_prices = !empty($usersDiscount['price']) ? $usersDiscount['price'] : [];
  555. $discount_level_ids = !empty($usersDiscount['level_id']) ? $usersDiscount['level_id'] : [];
  556. if (!empty($discount_level_ids[0])) {
  557. foreach ($discount_level_ids as $key => $value) {
  558. if (!empty($value)) {
  559. // 编辑
  560. if (!empty($discount_ids[$key])) {
  561. $update = [
  562. 'users_discount_id' => intval($discount_ids[$key]),
  563. 'aid' => intval($aid),
  564. 'level_id' => intval($value),
  565. 'users_discount_price' => !empty($discount_prices[$key]) ? floatval($discount_prices[$key]) : 0,
  566. 'update_time' => $times,
  567. ];
  568. $productUsersDiscount->update($update);
  569. }
  570. // 新增
  571. else {
  572. $insertAll[] = [
  573. 'aid' => intval($aid),
  574. 'level_id' => intval($value),
  575. 'users_discount_price' => !empty($discount_prices[$key]) ? floatval($discount_prices[$key]) : 0,
  576. 'add_time' => $times,
  577. 'update_time' => $times,
  578. ];
  579. }
  580. }
  581. }
  582. }
  583. // 存在新增的会员折扣价则执行添加
  584. !empty($insertAll) && $productUsersDiscount->insertAll($insertAll);
  585. }
  586. // 生成模板返回
  587. private function createUsersDiscountPriceTpl($usersLevelList = [], $discountList = [], $usersPrice = 0)
  588. {
  589. $trTpl = '';
  590. $usersDiscountPrice = !empty($usersPrice) ? floatval($usersPrice) : floatval(0);
  591. foreach ($usersLevelList as $key => $value) {
  592. // 会员折扣列表
  593. $discountFind = !empty($discountList[$value['level_id']]) ? $discountList[$value['level_id']] : [];
  594. $usersDiscountID = !empty($discountFind['users_discount_id']) ? intval($discountFind['users_discount_id']) : 0;
  595. $usersDiscountPrice = !empty($discountFind['users_discount_price']) ? floatval($discountFind['users_discount_price']) : floatval($usersPrice);
  596. // 模板拼装
  597. $trTpl .= <<<EOF
  598. <tr>
  599. <input type="hidden" name="users_discount[id][]" value="{$usersDiscountID}">
  600. <input type="hidden" name="users_discount[level_id][]" value="{$value['level_id']}">
  601. <td style='padding: 10px !important; width: 200px;'>
  602. <b style='font-weight: normal; color: #333;'>{$value['level_name']}</b>
  603. </td>
  604. <td style='padding: 10px !important;'>
  605. <input type='text' class='users_discount_price' name="users_discount[price][]" value="{$usersDiscountPrice}" onpaste='this.value=this.value.replace(/[^\d.]/g, "");' onkeyup='this.value=this.value.replace(/[^\d.]/g, "");'>&nbsp;元
  606. </td>
  607. </tr>
  608. EOF;
  609. }
  610. // 模板拼装
  611. $resultTpl = <<<EOF
  612. <table class='table table-bordered' border='1' cellpadding='10' cellspacing='10' style='border: 1px solid #ddd;'>
  613. <thead>
  614. <tr>
  615. <td style='padding: 10px !important; width: 200px;'><b style='font-weight: normal; color: #333;'>会员级别</b></td>
  616. <td style='padding: 10px !important; width: 200px;'><b style='font-weight: normal; color: #333;'>会员价格</b> &nbsp; <a href="javascript:void(0);" onclick="bulkSetUsersDiscountPrice(this);" >批量设置 </a></td>
  617. </tr>
  618. </thead>
  619. <tbody>
  620. {$trTpl}
  621. </tbody>
  622. </table>
  623. EOF;
  624. return $resultTpl;
  625. }
  626. /**
  627. * 订单是否改价
  628. * @param integer $order_id [description]
  629. * @return boolean [description]
  630. */
  631. public function is_change_price($order_id = 0)
  632. {
  633. // 订单是否改价过
  634. $is_change_price = 0;
  635. $orderLog = Db::name('shop_order_log')->field('action_desc')->where('order_id', $order_id)->find();
  636. if (!empty($orderLog) && stristr($orderLog['action_desc'], '改价')) {
  637. $is_change_price = 1;
  638. }
  639. return $is_change_price;
  640. }
  641. }