No Description
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.

Shop.php 120KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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-3-20
  12. */
  13. namespace app\user\controller;
  14. use think\Db;
  15. use think\Config;
  16. use think\Page;
  17. use think\Cookie;
  18. use app\common\logic\ShopCommonLogic;
  19. class Shop extends Base
  20. {
  21. // 初始化
  22. public function _initialize() {
  23. parent::_initialize();
  24. $this->users_db = Db::name('users'); // 会员数据表
  25. $this->users_money_db = Db::name('users_money'); // 会员金额明细表
  26. $this->shop_cart_db = Db::name('shop_cart'); // 购物车表
  27. $this->shop_order_db = Db::name('shop_order'); // 订单主表
  28. $this->shop_order_details_db = Db::name('shop_order_details'); // 订单明细表
  29. $this->shop_order_service_db = Db::name('shop_order_service'); // 订单售后服务表
  30. $this->shop_address_db = Db::name('shop_address'); // 收货地址表
  31. $this->archives_db = Db::name('archives'); // 产品表
  32. $this->product_attr_db = Db::name('product_attr'); // 产品属性表
  33. $this->product_attribute_db = Db::name('product_attribute'); // 产品属性标题表
  34. $this->region_db = Db::name('region'); // 三级联动地址总表
  35. $this->shipping_template_db = Db::name('shop_shipping_template'); // 运费模板表
  36. $this->shop_model = model('Shop'); // 商城模型
  37. $this->shop_common = new ShopCommonLogic(); // common商城业务层,前后台共用
  38. // 订单中心是否开启
  39. $redirect_url = '';
  40. $shop_open = getUsersConfigData('shop.shop_open');
  41. $web_users_switch = tpCache('web.web_users_switch');
  42. if (empty($shop_open)) {
  43. // 订单功能关闭,立马跳到会员中心
  44. $redirect_url = url('user/Users/index');
  45. $msg = '商城中心尚未开启!';
  46. } else if (empty($web_users_switch)) {
  47. // 前台会员中心已关闭,跳到首页
  48. $redirect_url = ROOT_DIR.'/';
  49. $msg = '会员中心尚未开启!';
  50. }
  51. if (!empty($redirect_url)) {
  52. Db::name('users_menu')->where([
  53. 'mca' => 'user/Shop/shop_centre',
  54. 'lang' => $this->home_lang,
  55. ])->update([
  56. 'status' => 0,
  57. 'update_time' => getTime(),
  58. ]);
  59. $this->error($msg, $redirect_url);
  60. exit;
  61. }
  62. // --end
  63. }
  64. // 购物车列表
  65. public function shop_cart_list()
  66. {
  67. // 数据由标签调取生成
  68. return $this->fetch('users/shop_cart_list');
  69. }
  70. // 订单管理列表,订单中心
  71. public function shop_centre()
  72. {
  73. $result = [];
  74. // 应用搜索条件
  75. $keywords = input('param.keywords/s');
  76. // 订单状态搜索
  77. $select_status = input('param.select_status');
  78. // 查询订单是否为空
  79. $result['data'] = $this->shop_model->GetOrderIsEmpty($this->users_id,$keywords,$select_status);
  80. // 是否移动端,1表示手机端,0表示PC端
  81. $result['IsMobile'] = isMobile() ? 1 : 0;
  82. // 菜单名称
  83. $result['title'] = Db::name('users_menu')->where([
  84. 'mca' => 'user/Shop/shop_centre',
  85. 'lang' => $this->home_lang,
  86. ])->getField('title');
  87. // 加载数据
  88. $eyou = [
  89. 'field' => $result,
  90. ];
  91. $this->assign('eyou',$eyou);
  92. return $this->fetch('users/shop_centre');
  93. }
  94. // 订单数据详情
  95. public function shop_order_details()
  96. {
  97. if (IS_GET) {
  98. // 数据由标签调取生成
  99. return $this->fetch('users/shop_order_details');
  100. }else{
  101. $this->error('非法访问!');
  102. }
  103. }
  104. // 订单提交
  105. public function shop_under_order($error = true)
  106. {
  107. if (empty($error)) {
  108. if ($this->usersTplVersion == 'v3') {
  109. $this->redirect(url('user/Shop/shop_cart_list'));
  110. } else {
  111. $this->error('您的购物车还没有商品!');
  112. }
  113. }
  114. // 获取当前页面URL,存入session,若操作添加地址后返回当前页面
  115. session($this->users_id.'_EyouShopOrderUrl', $this->request->url(true));
  116. // 数据由标签调取生成
  117. return $this->fetch('users/shop_under_order');
  118. }
  119. // 购物车库存检测
  120. public function cart_stock_detection()
  121. {
  122. if (IS_AJAX_POST) {
  123. // 购物车查询条件
  124. $CartWhere = [
  125. 'a.users_id' => $this->users_id,
  126. 'a.lang' => $this->home_lang,
  127. 'a.selected' => 1,
  128. ];
  129. $list = $this->shop_cart_db->field('a.product_num, b.stock_count, c.spec_value_id, c.spec_stock,b.prom_type')
  130. ->alias('a')
  131. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  132. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  133. ->where($CartWhere)
  134. ->order('a.add_time desc')
  135. ->select();
  136. if (empty($list)) $this->error('请选择要购买的商品!');
  137. $ExceedingStock = 0;
  138. // 处理商品库存检测
  139. foreach ($list as $key => $value) {
  140. // 购物车商品存在规格并且库存不为空,则覆盖商品原来的库存
  141. if (!empty($value['spec_value_id'])) {
  142. $value['stock_count'] = $value['spec_stock'];
  143. }
  144. // 检测是否有超出库存的产品
  145. if (empty($value['product_num']) || $value['product_num'] > $value['stock_count']) {
  146. $ExceedingStock = 1;
  147. break;
  148. }
  149. // 若库存为空则清除这条数据
  150. if (empty($value['stock_count'])) {
  151. unset($list[$key]);
  152. continue;
  153. }
  154. }
  155. $this->success('检测完成', null, $ExceedingStock);
  156. }
  157. }
  158. // 收货地址管理列表
  159. public function shop_address_list()
  160. {
  161. // 将当前URL存入 session ,使用微信获取收货地址时需要验签加密
  162. session($this->users_id.'_EyouShopAddAddress', $this->request->url(true));
  163. // 获取当前页面URL,存入session,若操作添加地址后返回当前页面
  164. session($this->users_id.'_EyouShopOrderUrl', $this->request->url(true));
  165. // 指定返回上一级URL
  166. $gourl = input('param.gourl/s');
  167. $gourl = urldecode($gourl);
  168. $this->assign('gourl', $gourl);
  169. $this->assign('add_addr_url', url("user/Shop/shop_get_wechat_addr"));
  170. // 收货地址列入的来源入口
  171. $type = input('param.type/s');
  172. $this->assign('type', $type);
  173. // 数据由标签调取生成
  174. return $this->fetch('users/shop_address_list');
  175. }
  176. // 取消订单
  177. public function shop_order_cancel()
  178. {
  179. if (IS_AJAX_POST) {
  180. $order_id = input('param.order_id/d');
  181. if (!empty($order_id)) {
  182. // 更新条件
  183. $Where = [
  184. 'order_id' => $order_id,
  185. 'users_id' => $this->users_id,
  186. 'lang' => $this->home_lang,
  187. ];
  188. // 更新数据
  189. $Data = [
  190. 'order_status' => '-1',
  191. 'update_time' => getTime(),
  192. ];
  193. // 更新订单主表
  194. $return = $this->shop_order_db->where($Where)->update($Data);
  195. if (!empty($return)) {
  196. // 订单取消,还原单内产品数量
  197. model('ProductSpecValue')->SaveProducSpecValueStock($order_id, $this->users_id);
  198. // 添加订单操作记录
  199. AddOrderAction($order_id,$this->users_id,'0','0','0','0','订单取消!','会员关闭订单!');
  200. $this->success('订单已取消!');
  201. }else{
  202. $this->error('操作失败!');
  203. }
  204. }
  205. }
  206. }
  207. // 立即购买
  208. public function shop_buy_now()
  209. {
  210. if (IS_AJAX_POST) {
  211. $param = input('param.');
  212. $param['aid'] = intval($param['aid']);
  213. $param['num'] = intval($param['num']);
  214. // 商品是否已售罄
  215. $this->IsSoldOut($param);
  216. // 数量不可为空
  217. if (empty($param['num']) || 0 > $param['num']) {
  218. $this->error('请选择数量!');
  219. }
  220. // 查询条件
  221. $archives_where = [
  222. 'arcrank' => array('egt','0'), //带审核稿件不查询
  223. 'aid' => $param['aid'],
  224. 'lang' => $this->home_lang,
  225. ];
  226. $count = $this->archives_db->where($archives_where)->count();
  227. // 跳转下单页
  228. if (!empty($count)) {
  229. // 对ID和订单号加密,拼装url路径
  230. $querydata = [
  231. 'aid' => $param['aid'],
  232. 'product_num' => $param['num'],
  233. ];
  234. /*若开启多规格则执行*/
  235. if (!empty($this->usersConfig['shop_open_spec']) && !empty($param['spec_value_id'])) {
  236. $querydata['spec_value_id'] = $param['spec_value_id'];
  237. }
  238. /* END */
  239. /*特定场景专用*/
  240. $opencodetype = config('global.opencodetype');
  241. if (1 == $opencodetype) {
  242. if (!empty($param['mini_id']) && intval($param['mini_id']) > 0) {
  243. $querydata['mini_id'] = intval($param['mini_id']);
  244. }
  245. }
  246. /*end*/
  247. // 先 json_encode 后 md5 加密信息
  248. $querystr = md5(json_encode($querydata));
  249. // 存入 cookie
  250. cookie($querystr, $querydata);
  251. // 跳转链接
  252. $url = urldecode(url('user/Shop/shop_under_order', ['querystr'=>$querystr]));
  253. $this->success('立即购买!',$url);
  254. }else{
  255. $this->error('该商品不存在或已下架!');
  256. }
  257. }else {
  258. $this->error('非法访问!');
  259. }
  260. }
  261. // 添加购物车数据
  262. public function shop_add_cart()
  263. {
  264. if (IS_AJAX_POST) {
  265. $param = input('param.');
  266. $param['aid'] = intval($param['aid']);
  267. // 商品是否已售罄
  268. $this->IsSoldOut($param);
  269. // 数量不可为空
  270. if (empty($param['num']) || 0 > $param['num']) $this->error('请选择数量!');
  271. // 查询条件
  272. $archives_where = [
  273. 'arcrank' => ['egt', 0],
  274. 'aid' => $param['aid'],
  275. 'lang' => $this->home_lang,
  276. ];
  277. $count = $this->archives_db->where($archives_where)->count();
  278. // 加入购物车处理
  279. if (!empty($count)) {
  280. // 查询条件
  281. $cart_where = [
  282. 'users_id' => $this->users_id,
  283. 'product_id' => $param['aid'],
  284. 'lang' => $this->home_lang,
  285. ];
  286. // 若开启多规格则执行
  287. if (!empty($this->usersConfig['shop_open_spec']) && !empty($param['spec_value_id'])) {
  288. $cart_where['spec_value_id'] = $param['spec_value_id'];
  289. }
  290. $cartInfo = $this->shop_cart_db->field('product_num')->where($cart_where)->find();
  291. if (!empty($cartInfo)) {
  292. // 购物车内已有相同产品,进行数量更新。
  293. $data['product_num'] = $param['num'] + intval($cartInfo['product_num']); //与购物车数量进行叠加
  294. $data['update_time'] = getTime();
  295. $cart_id = $this->shop_cart_db->where($cart_where)->update($data);
  296. } else {
  297. // 购物车内还未有相同产品,进行添加。
  298. $data['users_id'] = $this->users_id;
  299. $data['product_id'] = $param['aid'];
  300. $data['product_num'] = $param['num'];
  301. $data['spec_value_id'] = $param['spec_value_id'];
  302. $data['add_time'] = getTime();
  303. $cart_id = $this->shop_cart_db->add($data);
  304. }
  305. if (!empty($cart_id)) {
  306. $this->success('加入购物车成功!', url('user/Shop/shop_cart_list'), ['code'=>1]);
  307. } else {
  308. $this->error('加入购物车失败!', null, ['code'=>-1]);
  309. }
  310. } else {
  311. $this->error('该商品不存在或已下架!', null, ['code'=>-1]);
  312. }
  313. } else {
  314. $this->error('非法访问!', null, ['code'=>-1]);
  315. }
  316. }
  317. // 统一修改购物车数量
  318. // symbol 加或减数量或直接修改数量
  319. public function cart_unified_algorithm(){
  320. if (IS_AJAX_POST) {
  321. $aid = input('post.aid/d');
  322. $symbol = input('post.symbol');
  323. $num = input('post.num/d');
  324. $spec_value_id = input('post.spec_value_id');
  325. // 查询条件
  326. $archives_where = [
  327. 'arcrank' => array('egt','0'),
  328. 'aid' => $aid,
  329. 'lang' => $this->home_lang,
  330. ];
  331. $archives_count = $this->archives_db->where($archives_where)->count();
  332. if (!empty($archives_count)) {
  333. // 查询条件
  334. $cart_where = [
  335. 'users_id' => $this->users_id,
  336. 'product_id' => $aid,
  337. 'lang' => $this->home_lang,
  338. 'spec_value_id' => $spec_value_id,
  339. ];
  340. // 判断追加查询条件,当减数量时,商品数量最少为1
  341. if ('-' == $symbol) $cart_where['product_num'] = array('gt','1');
  342. $product_num = $this->shop_cart_db->where($cart_where)->getField('product_num');
  343. // 处理购物车产品数量
  344. if (!empty($product_num)) {
  345. // 更新数组
  346. if ('+' == $symbol) {
  347. $data['product_num'] = $product_num + 1;
  348. }else if ('-' == $symbol) {
  349. $data['product_num'] = $product_num - 1;
  350. }else if ('change' == $symbol) {
  351. $data['product_num'] = $num;
  352. }
  353. $data['update_time'] = getTime();
  354. // 更新数据
  355. $cart_id = $this->shop_cart_db->where($cart_where)->update($data);
  356. // 计算金额数量
  357. $CaerWhere = [
  358. 'a.users_id' => $this->users_id,
  359. 'a.lang' => $this->home_lang,
  360. 'a.selected' => 1,
  361. ];
  362. $CartData = $this->shop_cart_db
  363. ->field('a.product_num, b.users_price, c.spec_price')
  364. ->alias('a')
  365. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  366. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  367. ->where($CaerWhere)
  368. ->select();
  369. $level_discount = $this->users['level_discount'];
  370. $discount_price = $level_discount / 100;
  371. $spec_price = $users_price = $num_new = 0;
  372. foreach ($CartData as $key => $value) {
  373. if (!empty($value['spec_price'])) {
  374. if (!empty($level_discount)) $value['spec_price'] = $value['spec_price'] * $discount_price;
  375. $spec_price += $value['product_num'] * $value['spec_price'];
  376. } else {
  377. if (!empty($level_discount)) $value['users_price'] = $value['users_price'] * $discount_price;
  378. $users_price += $value['product_num'] * $value['users_price'];
  379. }
  380. $num_new += $value['product_num'];
  381. }
  382. $CartData['num'] = $num_new;
  383. $CartData['price'] = unifyPriceHandle($spec_price + $users_price);
  384. if (empty($CartData['num']) && empty($CartData['price'])) {
  385. $CartData['num'] = '0';
  386. $CartData['price'] = '0';
  387. }
  388. $CartAmountVal = $this->shop_cart_db->where([ 'users_id' => $this->users_id,'lang' => $this->home_lang])->sum('product_num');
  389. if (!empty($cart_id)) {
  390. $this->success('操作成功!', null, ['NumberVal'=>$CartData['num'], 'AmountVal'=>$CartData['price'], 'CartAmountVal'=>$CartAmountVal]);
  391. }
  392. } else {
  393. $this->error('商品数量最少为1', null, ['error'=>'0']);
  394. }
  395. } else {
  396. $this->error('该商品不存在或已下架!');
  397. }
  398. }
  399. }
  400. // 删除购物车内的产品
  401. public function cart_del()
  402. {
  403. if (IS_AJAX_POST) {
  404. $cart_id = input('post.cart_id/s');
  405. if (!empty($cart_id)) {
  406. // 删除条件
  407. $cart_where = [
  408. 'cart_id' => ['IN', explode(',', $cart_id)],
  409. 'users_id' => $this->users_id,
  410. 'lang' => $this->home_lang,
  411. ];
  412. // 删除数据
  413. $return = $this->shop_cart_db->where($cart_where)->delete();
  414. }
  415. if (!empty($return)) {
  416. // 计算购物车选中商品的总数总额
  417. $CaerWhere = [
  418. 'a.users_id' => $this->users_id,
  419. 'a.lang' => $this->home_lang,
  420. 'a.selected' => 1
  421. ];
  422. $CartData = $this->shop_cart_db
  423. ->field('a.product_num, b.users_price, c.spec_price')
  424. ->alias('a')
  425. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  426. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  427. ->where($CaerWhere)
  428. ->select();
  429. $level_discount = $this->users['level_discount'];
  430. $discount_price = $level_discount / 100;
  431. $spec_price = $users_price = $num_new = 0;
  432. foreach ($CartData as $key => $value) {
  433. if (!empty($value['spec_price'])) {
  434. if (!empty($level_discount)) $value['spec_price'] = $value['spec_price'] * $discount_price;
  435. $spec_price += $value['product_num'] * $value['spec_price'];
  436. } else {
  437. if (!empty($level_discount)) $value['users_price'] = $value['users_price'] * $discount_price;
  438. $users_price += $value['product_num'] * $value['users_price'];
  439. }
  440. $num_new += $value['product_num'];
  441. }
  442. $CartData['num'] = empty($num_new) ? 0 : $num_new;
  443. $CartData['price'] = unifyPriceHandle($spec_price + $users_price);
  444. $CartData['price'] = empty($CartData['price']) ? '0.00' : $CartData['price'];
  445. // 购物车是否还存在商品
  446. $CartCount = $this->shop_cart_db->where(['users_id' => $this->users_id, 'lang' => $this->home_lang])->count();
  447. // 购物车所有商品数量
  448. $CartAmountVal = $this->shop_cart_db->where(['users_id' => $this->users_id,'lang' => $this->home_lang])->sum('product_num');
  449. // 返回数据
  450. $data = [
  451. 'NumberVal' => $CartData['num'],
  452. 'AmountVal' => $CartData['price'],
  453. 'CartCount' => $CartCount,
  454. 'CartAmountVal' => $CartAmountVal,
  455. ];
  456. $this->success('操作成功!', null, $data);
  457. } else {
  458. $this->error('删除失败!');
  459. }
  460. }
  461. }
  462. // 删除全部选中的购物车商品
  463. public function select_cart_del()
  464. {
  465. if (IS_AJAX_POST) {
  466. // 删除条件
  467. $where = [
  468. 'selected' => 1,
  469. 'users_id' => $this->users_id,
  470. 'lang' => $this->home_lang,
  471. ];
  472. // 删除数据
  473. $result = $this->shop_cart_db->where($where)->delete(true);
  474. if (!empty($result)) {
  475. $this->success('删除成功');
  476. } else {
  477. $this->error('删除失败');
  478. }
  479. }
  480. }
  481. // 移入收藏
  482. public function move_to_collection()
  483. {
  484. if (IS_AJAX_POST) {
  485. $cart_id = input('post.cart_id');
  486. if (!empty($cart_id)) {
  487. // 删除条件
  488. $cart_where = [
  489. 'cart_id' => $cart_id,
  490. 'users_id' => $this->users_id,
  491. 'lang' => $this->home_lang,
  492. ];
  493. // 加入收藏
  494. $aid = $this->shop_cart_db->where($cart_where)->value('product_id');
  495. $row = Db::name('users_collection')->where(['users_id' => $this->users_id, 'aid' => $aid])->find();
  496. if (empty($row)) {
  497. $archivesInfo = Db::name('archives')->field('aid,title,litpic,channel,typeid')->find($aid);
  498. if (!empty($archivesInfo)) {
  499. Db::name('users_collection')->add([
  500. 'users_id' => $this->users_id,
  501. 'title' => $archivesInfo['title'],
  502. 'aid' => $aid,
  503. 'litpic' => $archivesInfo['litpic'],
  504. 'channel' => $archivesInfo['channel'],
  505. 'typeid' => $archivesInfo['typeid'],
  506. 'lang' => $this->home_lang,
  507. 'add_time' => getTime(),
  508. 'update_time' => getTime(),
  509. ]);
  510. }
  511. }
  512. // 删除数据
  513. $return = $this->shop_cart_db->where($cart_where)->delete();
  514. }
  515. if (!empty($return)) {
  516. // 计算购物车选中商品的总数总额
  517. $CaerWhere = [
  518. 'a.users_id' => $this->users_id,
  519. 'a.lang' => $this->home_lang,
  520. 'a.selected' => 1
  521. ];
  522. $CartData = $this->shop_cart_db
  523. ->field('a.product_num, b.users_price, c.spec_price')
  524. ->alias('a')
  525. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  526. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  527. ->where($CaerWhere)
  528. ->select();
  529. $level_discount = $this->users['level_discount'];
  530. $discount_price = $level_discount / 100;
  531. $spec_price = $users_price = $num_new = 0;
  532. foreach ($CartData as $key => $value) {
  533. if (!empty($value['spec_price'])) {
  534. if (!empty($level_discount)) $value['spec_price'] = $value['spec_price'] * $discount_price;
  535. $spec_price += $value['product_num'] * $value['spec_price'];
  536. } else {
  537. if (!empty($level_discount)) $value['users_price'] = $value['users_price'] * $discount_price;
  538. $users_price += $value['product_num'] * $value['users_price'];
  539. }
  540. $num_new += $value['product_num'];
  541. }
  542. $CartData['num'] = empty($num_new) ? 0 : $num_new;
  543. $CartData['price'] = unifyPriceHandle($spec_price + $users_price);
  544. $CartData['price'] = empty($CartData['price']) ? '0.00' : $CartData['price'];
  545. // 购物车是否还存在商品
  546. $CartCount = $this->shop_cart_db->where(['users_id' => $this->users_id, 'lang' => $this->home_lang])->count();
  547. // 返回数据
  548. $data = [
  549. 'NumberVal' => $CartData['num'],
  550. 'AmountVal' => $CartData['price'],
  551. 'CartCount' => $CartCount,
  552. ];
  553. $this->success('操作成功!', null, $data);
  554. } else {
  555. $this->error('操作失败!');
  556. }
  557. }
  558. }
  559. // 选中产品
  560. public function cart_checked()
  561. {
  562. if (IS_AJAX_POST) {
  563. $cart_id = input('post.cart_id/s', '');
  564. $selected = input('post.selected/d', 0);
  565. // 更新数组
  566. if (!empty($selected)) {
  567. $selected = 0;
  568. } else {
  569. $selected = 1;
  570. }
  571. $update['selected'] = $selected;
  572. $update['update_time'] = getTime();
  573. // 更新条件
  574. if ('*' == $cart_id) {
  575. $where = [
  576. 'product_num' => ['>', 0],
  577. 'users_id' => $this->users_id,
  578. 'lang' => $this->home_lang,
  579. ];
  580. } else {
  581. $where = [
  582. 'cart_id' => intval($cart_id),
  583. 'product_num' => ['>', 0],
  584. 'users_id' => $this->users_id,
  585. 'lang' => $this->home_lang,
  586. ];
  587. }
  588. // 更新数据
  589. $return = $this->shop_cart_db->where($where)->update($update);
  590. if (!empty($return)) {
  591. $this->success('操作成功!');
  592. } else {
  593. $this->error('操作失败!');
  594. }
  595. }
  596. }
  597. public function shop_wechat_pay_select()
  598. {
  599. $ReturnOrderData = session($this->users_id.'_ReturnOrderData');
  600. if (empty($ReturnOrderData)) {
  601. $url = session($this->users_id.'_EyouShopOrderUrl');
  602. $this->error('订单支付异常,请刷新重新下单~',$url);
  603. }
  604. $eyou = [
  605. 'field' => $ReturnOrderData,
  606. ];
  607. $this->assign('eyou',$eyou);
  608. return $this->fetch('users/shop_wechat_pay_select');
  609. }
  610. /**
  611. * 快速下单支付流程 - 添加商品信息及计算价格等
  612. * @return [type] [description]
  613. */
  614. public function fastSubmitOrder()
  615. {
  616. $aid = input('post.aid_v507428/d');
  617. $pay_code = input('post.pay_code_v507428/s'); // 支付方式
  618. if (IS_POST && !empty($aid) && !empty($pay_code)) {
  619. $OrderData = [];
  620. $mini_id = input('post.mini_id_v507428/d');
  621. !empty($mini_id) && $OrderData['mini_id'] = $mini_id;
  622. // 规格值
  623. $spec_value_id = input('post.spec_value_id_v507428/s');
  624. if (!empty($spec_value_id)) {
  625. $spec_value_id = preg_replace('/[^\d\_]/i', '', $spec_value_id);
  626. $spec_value_id = trim($spec_value_id, '_');
  627. $OrderData['spec_value_id'] = "_{$spec_value_id}_";
  628. }
  629. $ArchivesWhere = [
  630. 'a.aid' => $aid,
  631. ];
  632. if (!empty($spec_value_id)) $ArchivesWhere['b.spec_value_id'] = $spec_value_id;
  633. $field = 'a.aid, a.aid as product_id, a.title, a.litpic, a.users_price, a.stock_count, a.prom_type, a.attrlist_id, b.spec_price, b.spec_stock, b.spec_value_id, b.value_id';
  634. $list = Db::name('archives')->field($field)
  635. ->alias('a')
  636. ->join('__PRODUCT_SPEC_VALUE__ b', 'a.aid = b.aid', 'LEFT')
  637. ->where($ArchivesWhere)
  638. ->limit('0, 1')
  639. ->select();
  640. // 没有相应的产品
  641. if (empty($list[0])) $this->error('订单生成失败,没有相应的商品!');
  642. $list[0]['product_num'] = 1;
  643. $list[0]['under_order_type'] = 2; // 快速下单支付
  644. // 生成订单之前的产品数据整理
  645. $retData = $this->shop_model->handlerOrderData('fast', $OrderData, $list, $this->users);
  646. if (empty($retData['code'])) {
  647. $this->error($retData['msg']);
  648. }
  649. $OrderData = array_merge($OrderData, $retData['data']['OrderData']);
  650. $list = $retData['data']['list'];
  651. $OrderId = Db::name('shop_order')->insertGetId($OrderData);
  652. $OrderData['order_id'] = $OrderId;
  653. if (!empty($OrderId)) {
  654. // 生成订单之后的订单明细整理
  655. $retData = $this->shop_model->handlerDetailsData('fast', $OrderData, $list, $this->users);
  656. if (empty($retData['code'])) {
  657. $this->error($retData['msg']);
  658. }
  659. $cart_ids = $retData['data']['cart_ids'];
  660. $OrderDetailsData = $retData['data']['OrderDetailsData'];
  661. $UpSpecValue = $retData['data']['UpSpecValue'];
  662. $DetailsId = Db::name('shop_order_details')->insertAll($OrderDetailsData);
  663. if (!empty($DetailsId)) {
  664. // 清理购物车中已下单的ID
  665. if (!empty($cart_ids)) Db::name('shop_cart')->where('cart_id', 'IN', $cart_ids)->delete();
  666. // 产品库存、销量处理
  667. $this->shop_model->ProductStockProcessing($UpSpecValue);
  668. // 添加订单操作记录
  669. AddOrderAction($OrderId, $this->users_id);
  670. if (0 == $OrderData['payment_method']) {
  671. // 选择在线付款并且在手机微信端、小程序中则返回订单ID,订单号,订单交易类型
  672. if (isMobile() && isWeixin()) {
  673. // $where = [
  674. // 'pay_id' => 1,
  675. // 'pay_mark' => 'wechat'
  676. // ];
  677. // $PayInfo = Db::name('pay_api_config')->where($where)->getField('pay_info');
  678. // if (!empty($PayInfo)) $PayInfo = unserialize($PayInfo);
  679. // if (!empty($this->users['open_id']) && !empty($PayInfo) && 0 == $PayInfo['is_open_wechat']) {
  680. // $ReturnOrderData = [
  681. // 'unified_id' => $OrderId,
  682. // 'unified_number' => $OrderData['order_code'],
  683. // 'transaction_type' => 2, // 订单支付购买
  684. // 'order_total_amount' => $TotalAmount,
  685. // 'order_source' => 1, // 提交订单页
  686. // 'is_gourl' => 1,
  687. // ];
  688. // if ($this->users['users_money'] <= '0.00') {
  689. // // 余额为0
  690. // $ReturnOrderData['is_gourl'] = 0;
  691. // $this->success('订单已生成!', null, $ReturnOrderData);
  692. // } else {
  693. // // 余额不为0
  694. // $url = url('user/Shop/shop_wechat_pay_select');
  695. // session($this->users_id.'_ReturnOrderData', $ReturnOrderData);
  696. // $this->success('订单已生成!', $url, $ReturnOrderData);
  697. // }
  698. // } else {
  699. // // 如果会员没有openid则跳转到支付页面进行支付
  700. // // 在线付款时,跳转至付款页
  701. // // 对ID和订单号加密,拼装url路径
  702. // $Paydata = [
  703. // 'order_id' => $OrderId,
  704. // 'order_code' => $OrderData['order_code'],
  705. // ];
  706. // // 先 json_encode 后 md5 加密信息
  707. // $Paystr = md5(json_encode($Paydata));
  708. // // 存入 cookie
  709. // cookie($Paystr, $Paydata);
  710. // // 跳转链接
  711. // $PaymentUrl = urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$Paystr]));
  712. // $this->success('订单已生成!', $PaymentUrl, ['is_gourl' => 1]);
  713. // }
  714. } else {
  715. if ('alipay' == $pay_code) {
  716. // 重要参数,支付宝配置信息
  717. $PayInfo = Db::name('pay_api_config')->where([
  718. 'pay_id' => 2,
  719. 'pay_mark' => 'alipay'
  720. ])->getField('pay_info');
  721. if (empty($PayInfo)) $this->error('请先配置支付宝!');
  722. $alipay = unserialize($PayInfo);
  723. $vars = [
  724. 'unified_number' => $OrderData['order_code'],
  725. 'unified_amount' => $OrderData['order_amount'],
  726. 'transaction_type' => 2,
  727. ];
  728. $PayApiLogic = new \app\user\logic\PayApiLogic;
  729. $retData = $PayApiLogic->UseAliPayPay(['transaction_type'=>2], $vars, $alipay, true);
  730. if (!empty($retData['alipay_url'])) {
  731. $PaymentUrl = $retData['alipay_url'];
  732. $this->redirect($PaymentUrl);
  733. } else {
  734. $this->error('调起支付宝页面失败!');
  735. }
  736. }
  737. else if ('weipay' == $pay_code) {
  738. $vars = [
  739. 'unified_number' => $OrderData['order_code'],
  740. 'transaction_type' => 2
  741. ];
  742. $data = [
  743. 'url_qrcode' => url('user/PayApi/pay_wechat_png', $vars),
  744. 'pay_id' => 1,
  745. 'pay_mark' => 'wechat',
  746. 'unified_id' => $OrderId,
  747. 'unified_number' => $OrderData['order_code'],
  748. 'transaction_type' => 2,
  749. ];
  750. $this->success('正在支付中', null, $data);
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. $this->error('操作失败!');
  758. }
  759. // 订单提交处理逻辑,添加商品信息及计算价格等
  760. public function shop_payment_page()
  761. {
  762. if (IS_POST) {
  763. // 提交的订单信息判断
  764. $post = input('post.');
  765. if (empty($post)) $this->error('订单生成失败,商品数据有误');
  766. // 物流类型
  767. $post['logistics_type'] = !empty($post['logistics_type']) ? intval($post['logistics_type']) : 0;
  768. if (2 === intval($post['logistics_type'])) {
  769. if (empty($post['store_id'])) $this->error('请选择自提门店');
  770. if (empty($post['buyer'])) $this->error('请输入用户姓名');
  771. if (empty($post['phone'])) $this->error('请输入预留手机号');
  772. if (!check_mobile($post['phone'])) $this->error('预留手机号格式不正确');
  773. }
  774. $OrderData = [];
  775. // 产品ID是否存在
  776. $Md5Value = !empty($post['Md5Value']) ? $post['Md5Value'] : null;
  777. if (!empty($Md5Value)) {
  778. $querystr = cookie($Md5Value);
  779. $aid = !empty($querystr['aid']) ? intval($querystr['aid']) : 0;
  780. $num = !empty($querystr['product_num']) ? intval($querystr['product_num']) : 0;
  781. $mini_id = !empty($querystr['mini_id']) ? intval($querystr['mini_id']) : 0;
  782. !empty($mini_id) && $OrderData['mini_id'] = $mini_id;
  783. $spec_value_id = !empty($querystr['spec_value_id']) ? $querystr['spec_value_id'] : '';
  784. !empty($spec_value_id) && $OrderData['spec_value_id'] = $spec_value_id;
  785. $type = !empty($post['type']) ? intval($post['type']) : 0;
  786. $OrderData['order_md5'] = md5($aid.$spec_value_id);
  787. // 商品数量判断
  788. if ($num <= 0) $this->error('订单生成失败,商品数量有误!');
  789. // 订单来源判断
  790. if ($type != 1) $this->error('订单生成失败,提交来源有误!');
  791. // 立即购买查询条件
  792. $where = [
  793. 'a.aid' => $aid,
  794. ];
  795. if (!empty($spec_value_id)) $where['b.spec_value_id'] = $spec_value_id;
  796. $field = 'a.aid, a.aid as product_id, a.title, a.litpic, a.users_price, a.users_discount_type, a.stock_count, a.prom_type, a.attrlist_id, b.spec_price, b.spec_stock, b.spec_value_id, b.value_id, c.spec_is_select, a.merchant_id, a.free_shipping';
  797. $list = $this->archives_db->field($field)
  798. ->alias('a')
  799. ->join('__PRODUCT_SPEC_VALUE__ b', 'a.aid = b.aid', 'LEFT')
  800. ->join('__PRODUCT_SPEC_DATA__ c', 'a.aid = c.aid and b.spec_value_id = c.spec_value_id', 'LEFT')
  801. ->where($where)
  802. ->limit('0, 1')
  803. ->select();
  804. $list[0]['product_num'] = $num;
  805. $list[0]['under_order_type'] = $type;
  806. if (empty($list[0]['spec_is_select'])) {
  807. $list[0]['spec_price'] = '';
  808. $list[0]['spec_stock'] = '';
  809. $list[0]['spec_value_id'] = '';
  810. }
  811. } else {
  812. // 购物车查询条件
  813. $where = [
  814. 'a.users_id' => $this->users_id,
  815. 'a.selected' => 1,
  816. ];
  817. $field = 'a.*, b.aid, b.title, b.litpic, b.users_price, b.users_discount_type, b.stock_count, b.prom_type, b.attrlist_id, c.spec_price, c.spec_stock, c.value_id, b.merchant_id, b.free_shipping';
  818. $list = $this->shop_cart_db->field($field)
  819. ->alias('a')
  820. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  821. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  822. ->where($where)
  823. ->select();
  824. }
  825. // 没有相应的产品
  826. if (empty($list)) $this->error('订单生成失败,没有相应的产品!');
  827. // 生成订单之前的产品数据整理
  828. $retData = $this->shop_model->handlerOrderData('normal', $OrderData, $list, $this->users, $post);
  829. if (empty($retData['code'])) $this->error($retData['msg']);
  830. // 判断下单商品是否有多个商家的商品,若多个商家一起下单则执行
  831. $merchantID = count(array_unique(get_arr_column($list, 'merchant_id')));
  832. if (intval($merchantID) > 1) {
  833. $multiMerchantLogic = new \app\user\logic\MultiMerchantLogic;
  834. $multiMerchantLogic->multiMerchantOrderHandle($retData['data']['list'], $post);
  835. }
  836. // 合并订单数据
  837. $OrderData = array_merge($OrderData, $retData['data']['OrderData']);
  838. $list = $retData['data']['list'];
  839. // 订单物流类型(0:虚拟订单; 1:快递发货; 2:到店核销;)
  840. $OrderData['logistics_type'] = !empty($post['logistics_type']) ? intval($post['logistics_type']) : 1;
  841. $post['verifyShow'] = $post['deliveryShow'] = false;
  842. if (1 === intval($OrderData['logistics_type'])) {
  843. $post['deliveryShow'] = true;
  844. } else if (2 === intval($OrderData['logistics_type'])) {
  845. $post['verifyShow'] = true;
  846. }
  847. $AddrData = [];
  848. // 非虚拟订单则查询运费信息
  849. if (empty($OrderData['prom_type']) && 1 === intval($OrderData['logistics_type'])) {
  850. // 没有选择收货地址
  851. if (empty($post['addr_id'])) {
  852. // 在微信端并且不在小程序中
  853. if (isWeixin() && !isWeixinApplets()) {
  854. // 跳转至收货地址添加选择页
  855. if ('v3' == $this->usersTplVersion) {
  856. $get_addr_url = url('user/Shop/shop_add_address', ['type'=>'order']);
  857. } else {
  858. $get_addr_url = url('user/Shop/shop_get_wechat_addr');
  859. }
  860. $is_gourl['is_gourl'] = 1;
  861. $this->success('101:选择添加地址方式', $get_addr_url, $is_gourl);
  862. } else {
  863. $paramNew['add_addr'] = 1;
  864. $paramNew['is_mobile'] = $this->is_mobile;
  865. if ('v3' == $this->usersTplVersion) {
  866. $paramNew['url'] = url('user/Shop/shop_add_address', ['type'=>'order']);
  867. }
  868. $this->error('101:订单生成失败,请添加收货地址', null, $paramNew);
  869. }
  870. }
  871. // 查询收货地址
  872. $AddrWhere = [
  873. 'addr_id' => $post['addr_id'],
  874. 'users_id' => $this->users_id,
  875. ];
  876. $AddressData = $this->shop_address_db->where($AddrWhere)->find();
  877. if (empty($AddressData)) {
  878. if (isWeixin() && !isWeixinApplets()) {
  879. // 跳转至收货地址添加选择页
  880. if ('v3' == $this->usersTplVersion) {
  881. $get_addr_url = url('user/Shop/shop_add_address', ['type'=>'order']);
  882. } else {
  883. $get_addr_url = url('user/Shop/shop_get_wechat_addr');
  884. }
  885. $is_gourl['is_gourl'] = 1;
  886. $this->success('102:选择添加地址方式', $get_addr_url, $is_gourl);
  887. } else {
  888. $paramNew['add_addr'] = 1;
  889. $paramNew['is_mobile'] = $this->is_mobile;
  890. if ('v3' == $this->usersTplVersion) {
  891. $paramNew['url'] = url('user/Shop/shop_add_address', ['type'=>'order']);
  892. }
  893. $this->error('102:订单生成失败,请添加收货地址', null, $paramNew);
  894. }
  895. }
  896. $shop_open_shipping = getUsersConfigData('shop.shop_open_shipping');
  897. $template_money = 0;
  898. if (!empty($shop_open_shipping)) {
  899. // 通过省份获取运费模板中的运费价格
  900. $template_money = $this->shipping_template_db->where('province_id', $AddressData['province'])->getField('template_money');
  901. if (0 >= $template_money) {
  902. // 省份运费价格为0时,使用统一的运费价格,固定ID为100000
  903. $template_money = $this->shipping_template_db->where('province_id', '100000')->getField('template_money');
  904. }
  905. // 合计金额加上运费价格
  906. $OrderData['order_total_amount'] += $template_money;
  907. $OrderData['order_amount'] += $template_money;
  908. }
  909. // 拼装数组
  910. $AddrData = [
  911. 'consignee' => $AddressData['consignee'],
  912. 'country' => $AddressData['country'],
  913. 'province' => $AddressData['province'],
  914. 'city' => $AddressData['city'],
  915. 'district' => $AddressData['district'],
  916. 'address' => $AddressData['address'],
  917. 'mobile' => $AddressData['mobile'],
  918. 'shipping_fee' => $template_money,
  919. ];
  920. }
  921. // 存在收货地址则追加合并到主表数组
  922. if (!empty($AddrData)) $OrderData = array_merge($OrderData, $AddrData);
  923. if (0 < $OrderData['order_amount']) {
  924. if (empty($post['payment_type'])) $this->error('网站支付配置未完善,购买服务暂停使用');
  925. // 数据验证
  926. $rule = ['payment_type' => 'require|token'];
  927. $message = ['payment_type.require' => '不可为空!'];
  928. $validate = new \think\Validate($rule, $message);
  929. if (!$validate->check($post)) $this->error('不可连续提交订单!');
  930. }
  931. // 订单提交处理 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  932. $OrderData = model('ShopPublicHandle')->orderSubmitPublicHandle($OrderData, $this->usersConfig, $this->users_id, $post, $list);
  933. // // 如果后台【商城中心】-【商城配置】-【订单设置】-收货后可维权时间设置为0,则表示订单不允许申请维权
  934. // if (empty($this->usersConfig['order_right_protect_time'])) $OrderData['allow_service'] = 1;
  935. // // 获取消费获得积分数据
  936. // $OrderData['obtain_scores'] = getConsumObtainScores($OrderData, $this->usersConfig, true);
  937. // $OrderData['is_obtain_scores'] = !empty($OrderData['obtain_scores']) ? 0 : 1;
  938. // 添加订单及后续处理
  939. $OrderId = $this->shop_order_db->insertGetId($OrderData);
  940. if (!empty($OrderId)) {
  941. $OrderId = intval($OrderId);
  942. $OrderData['order_id'] = $OrderId;
  943. // 订单创建后续处理 -- 其他逻辑公共调用方法,部分逻辑改动不适合直接修改原文件时请在此方法做处理和兼容
  944. $OrderData = model('ShopPublicHandle')->orderCreatePublicHandle($OrderData, $this->usersConfig, $this->users_id, $post, $list);
  945. // 生成订单之后的订单明细整理
  946. $retData = $this->shop_model->handlerDetailsData('normal', $OrderData, $list, $this->users);
  947. if (empty($retData['code'])) $this->error($retData['msg']);
  948. // 清理购物车中已下单的ID
  949. $cart_ids = $retData['data']['cart_ids'];
  950. // 产品库存、销量处理
  951. $UpSpecValue = $retData['data']['UpSpecValue'];
  952. // 添加订单明细表
  953. $OrderDetailsData = $retData['data']['OrderDetailsData'];
  954. $DetailsId = $this->shop_order_details_db->insertAll($OrderDetailsData);
  955. if (!empty($DetailsId)) {
  956. // 清理购物车中已下单的ID
  957. if (!empty($cart_ids)) $this->shop_cart_db->where('cart_id', 'IN', $cart_ids)->delete();
  958. // 产品库存、销量处理
  959. $this->shop_model->ProductStockProcessing($UpSpecValue);
  960. // 添加订单操作记录
  961. AddOrderAction($OrderId, $this->users_id);
  962. // 统计销售额
  963. eyou_statistics_data(2);
  964. eyou_statistics_data(3, $OrderData['order_amount']);
  965. // 第一版会员中心,清除下单的Cookie数据
  966. if ($this->usersTplVersion == 'v1' && !empty($Md5Value)) Cookie::delete($Md5Value);
  967. // 0元下单逻辑
  968. if (0 == floatval($OrderData['order_amount'])) {
  969. $pay_details = [
  970. 'unified_id' => $OrderData['order_id'],
  971. 'unified_number' => $OrderData['order_code'],
  972. 'transaction_type' => 2,
  973. 'payment_amount' => $OrderData['order_amount'],
  974. ];
  975. $returnData = pay_success_logic($this->users_id, $OrderData['order_code'], $pay_details, '');
  976. if (is_array($returnData)) {
  977. if (1 == $returnData['code']) {
  978. $this->success($returnData['msg'], $returnData['url'], $returnData['data']);
  979. } else {
  980. $this->error($returnData['msg'], null, ['url'=>url('user/Shop/shop_centre')]);
  981. }
  982. }
  983. }
  984. // 在线支付 -- 根据版本调用支付
  985. if (0 == $post['payment_method']) {
  986. // 手机微信端支付
  987. if (isMobile() && isWeixin()) {
  988. // 选择在线付款并且在手机微信端、小程序中则返回订单ID,订单号,订单交易类型
  989. $where = [
  990. 'pay_id' => 1,
  991. 'pay_mark' => 'wechat'
  992. ];
  993. $PayInfo = Db::name('pay_api_config')->where($where)->getField('pay_info');
  994. if (!empty($PayInfo)) $PayInfo = unserialize($PayInfo);
  995. if ((!empty($this->users['open_id']) || 0 === intval($this->users['thirdparty'])) && !empty($PayInfo) && 0 === intval($PayInfo['is_open_wechat'])) {
  996. $payment_type = $post['payment_type'];
  997. if ('yezf_balance' == $payment_type) {
  998. // 余额支付
  999. if ($this->users['users_money'] < $OrderData['order_amount']) {
  1000. // 余额不足,支付失败
  1001. $this->error('余额不足,支付失败!', null, ['url' => url('user/Shop/shop_centre')]);
  1002. } else {
  1003. // 余额充足,进行支付
  1004. $ret = Db::name('users')->where(['users_id' => $this->users_id])->update([
  1005. 'users_money' => Db::raw('users_money-'.$OrderData['order_amount']),
  1006. 'update_time' => getTime(),
  1007. ]);
  1008. if (false !== $ret) {
  1009. $pay_details = [
  1010. 'unified_id' => $OrderData['order_id'],
  1011. 'unified_number' => $OrderData['order_code'],
  1012. 'transaction_type' => 2,
  1013. 'payment_amount' => $OrderData['order_amount'],
  1014. 'payment_type' => "余额支付",
  1015. ];
  1016. $returnData = pay_success_logic($this->users_id, $OrderData['order_code'], $pay_details, 'balance', true, $this->users);
  1017. if (is_array($returnData)) {
  1018. if (1 == $returnData['code']) {
  1019. $this->success($returnData['msg'], $returnData['url'], $returnData['data']);
  1020. } else {
  1021. $this->error($returnData['msg'], null, ['url'=>url('user/Shop/shop_centre')]);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. } else {
  1027. // 微信内支付、小程序内支付 -- 微信端不存在支付宝支付
  1028. $ReturnOrderData = [
  1029. 'unified_id' => $OrderId,
  1030. 'unified_number' => $OrderData['order_code'],
  1031. 'transaction_type' => 2, // 订单支付购买
  1032. 'order_source' => 1, // 提交订单页
  1033. 'is_gourl' => 0, // 1,
  1034. 'order_total_amount' => 0 // 好像没有用处了
  1035. ];
  1036. $this->success('订单已生成!', null, $ReturnOrderData);
  1037. // if ($this->users['users_money'] <= '0.00' || $this->usersTplVersion == 'v3') {
  1038. // // 余额为0
  1039. // $ReturnOrderData['is_gourl'] = 0;
  1040. // $this->success('订单已生成!', null, $ReturnOrderData);
  1041. // } else {
  1042. // // 余额不为0
  1043. // $url = url('user/Shop/shop_wechat_pay_select');
  1044. // session($this->users_id.'_ReturnOrderData', $ReturnOrderData);
  1045. // $this->success('订单已生成!', $url, $ReturnOrderData);
  1046. // }
  1047. }
  1048. }
  1049. else {
  1050. // 执行如果会员没有openid则跳转到支付页面进行支付
  1051. $paydata = [
  1052. 'order_id' => $OrderId,
  1053. 'order_code' => $OrderData['order_code'],
  1054. ];
  1055. // 先 json_encode 后 md5 加密信息
  1056. $paystr = md5(json_encode($paydata));
  1057. // 存入 cookie
  1058. cookie($paystr, $paydata);
  1059. // 跳转链接
  1060. $this->success('订单已生成!', urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$paystr])), ['is_gourl' => 1]);
  1061. }
  1062. }
  1063. // PC端支付、手机浏览器端支付
  1064. else {
  1065. if (in_array($this->usersTplVersion, ['v2', 'v3', 'v4']) && isset($post['payment_type'])) {
  1066. // 余额支付、微信支付、支付宝支付、其他第三方支付
  1067. $payment_type = $post['payment_type'];
  1068. if ('yezf_balance' == $payment_type) {
  1069. // 余额支付
  1070. if ($this->users['users_money'] < $OrderData['order_amount']) {
  1071. $url = url('user/Shop/shop_centre');
  1072. $this->error('余额不足,支付失败!', null, ['url'=>$url]);
  1073. } else {
  1074. $ret = Db::name('users')->where(['users_id'=>$this->users_id])->update([
  1075. 'users_money' => Db::raw('users_money-'.$OrderData['order_amount']),
  1076. 'update_time' => getTime(),
  1077. ]);
  1078. if (false !== $ret) {
  1079. $pay_details = [
  1080. 'unified_id' => $OrderData['order_id'],
  1081. 'unified_number' => $OrderData['order_code'],
  1082. 'transaction_type' => 2,
  1083. 'payment_amount' => $OrderData['order_amount'],
  1084. 'payment_type' => "余额支付",
  1085. ];
  1086. $returnData = pay_success_logic($this->users_id, $OrderData['order_code'], $pay_details, 'balance', true, $this->users);
  1087. if (is_array($returnData)) {
  1088. if (1 == $returnData['code']) {
  1089. $this->success($returnData['msg'], $returnData['url'], $returnData['data']);
  1090. } else {
  1091. $this->error($returnData['msg'], null, ['url'=>url('user/Shop/shop_centre')]);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. } else if (in_array($payment_type, ['zxzf_wechat', 'zxzf_alipay', 'zxzf_Paypal', 'zxzf_UnionPay'])) {
  1097. // 内置第三方在线支付
  1098. $payment_type_arr = explode('_', $payment_type);
  1099. $pay_mark = !empty($payment_type_arr[1]) ? $payment_type_arr[1] : '';
  1100. $payApiRow = Db::name('pay_api_config')->where(['pay_mark' => $pay_mark, 'lang' => $this->home_lang])->find();
  1101. if (empty($payApiRow)) $this->error('请选择正确的支付方式!');
  1102. // 返回支付所需参数
  1103. $data = [
  1104. 'code' => 'order_status_0',
  1105. 'pay_id' => $payApiRow['pay_id'],
  1106. 'pay_mark' => $pay_mark,
  1107. 'unified_id' => $OrderData['order_id'],
  1108. 'unified_number' => $OrderData['order_code'],
  1109. 'transaction_type' => 2,
  1110. ];
  1111. $this->success('正在支付中', url('user/Shop/shop_centre'), $data);
  1112. }
  1113. }
  1114. else {
  1115. // 第一套模板 -- 执行跳转至订单支付页
  1116. $paydata = [
  1117. 'order_id' => $OrderId,
  1118. 'order_code' => $OrderData['order_code'],
  1119. ];
  1120. // 先 json_encode 后 md5 加密信息
  1121. $paystr = md5(json_encode($paydata));
  1122. // 存入 cookie
  1123. cookie($paystr, $paydata);
  1124. // 跳转链接
  1125. $this->success('订单已生成!', urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$paystr])));
  1126. }
  1127. }
  1128. }
  1129. // 货到付款 -- 跳转订单列表页
  1130. else {
  1131. // 再次添加一条订单操作记录
  1132. AddOrderAction($OrderId, $this->users_id, 0, 1, 0, 1, '货到付款', '会员选择货到付款,款项由快递代收');
  1133. // 邮箱发送
  1134. $SmtpConfig = tpCache('smtp');
  1135. $ReturnData['email'] = GetEamilSendData($SmtpConfig, $this->users, $OrderData, 1, 'delivery_pay');
  1136. // 手机发送
  1137. $SmsConfig = tpCache('sms');
  1138. $ReturnData['mobile'] = GetMobileSendData($SmsConfig, $this->users, $OrderData, 1, 'delivery_pay');
  1139. // 发送站内信给后台
  1140. $OrderData['pay_method'] = '货到付款';
  1141. SendNotifyMessage($OrderData, 5, 1, 0);
  1142. // 返回结束
  1143. $ReturnData['is_gourl'] = 1;
  1144. // 返回提示
  1145. $this->success('订单已生成!', urldecode(url('user/Shop/shop_centre')), $ReturnData);
  1146. }
  1147. } else {
  1148. $this->error('订单生成失败,商品数据有误!');
  1149. }
  1150. } else {
  1151. $this->error('订单生成失败,商品数据有误!');
  1152. }
  1153. }
  1154. }
  1155. public function to_split_goods()
  1156. {
  1157. if (IS_AJAX_POST) {
  1158. $weappInfo = model('ShopPublicHandle')->getWeappVerifyInfo();
  1159. if (empty($weappInfo)) {
  1160. $this->success('没有安装核销插件,直接进入购买');
  1161. } else if (1 !== intval($weappInfo['status'])) {
  1162. $this->success('没有启用核销插件,直接进入购买');
  1163. } else if (0 === intval($weappInfo['data']['openVerify'])) {
  1164. $this->success('没有开启核销功能,直接进入购买');
  1165. }
  1166. }
  1167. // 查询条件
  1168. $where = [
  1169. 'a.selected' => 1,
  1170. 'a.users_id' => $this->users_id,
  1171. 'a.lang' => $this->home_lang,
  1172. 'b.arcrank' => array('egt','0'),
  1173. ];
  1174. $field = 'a.*, b.aid, b.title, b.litpic, b.users_price, b.logistics_type, b.users_discount_type, b.stock_count, b.attrlist_id, b.is_del, c.spec_price, c.spec_stock';
  1175. $list = Db::name("shop_cart")
  1176. ->field($field)
  1177. ->alias('a')
  1178. ->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')
  1179. ->join('__PRODUCT_SPEC_VALUE__ c', 'a.spec_value_id = c.spec_value_id and a.product_id = c.aid', 'LEFT')
  1180. ->where($where)
  1181. ->order('a.selected desc, a.add_time desc')
  1182. ->select();
  1183. $verify = $delivery = [];
  1184. // 商品处理
  1185. foreach ($list as $key => $value) {
  1186. // 图片处理
  1187. $value['litpic'] = !empty($value['litpic']) ? handle_subdir_pic($value['litpic']) : '';
  1188. // 购物车商品存在规格并且价格不为空,则覆盖商品原来的价格
  1189. if (!empty($value['spec_value_id']) && !empty($value['spec_price'])) $value['users_price'] = unifyPriceHandle($value['spec_price']);
  1190. // 商品价格处理
  1191. $value['subtotal'] = 0;
  1192. if (!empty($value['users_price'])) {
  1193. // 计算小计
  1194. $value['subtotal'] = $value['users_price'] * $value['product_num'];
  1195. $value['subtotal'] = unifyPriceHandle($value['subtotal']);
  1196. }
  1197. // 虚拟商品
  1198. if ('0' === $value['logistics_type']) {
  1199. array_push($verify, $value);
  1200. array_push($delivery, $value);
  1201. }
  1202. // 仅物流配送
  1203. else if ('1' === $value['logistics_type']) {
  1204. array_push($delivery, $value);
  1205. }
  1206. // 仅到店核销
  1207. else if ('2' === $value['logistics_type']) {
  1208. array_push($verify, $value);
  1209. }
  1210. // 物流配送 和 到店核销
  1211. else {
  1212. $logisticsType = isset($value['logistics_type']) ? explode(',', $value['logistics_type']) : [];
  1213. if (in_array(1, $logisticsType) && in_array(2, $logisticsType)) {
  1214. array_push($verify, $value);
  1215. array_push($delivery, $value);
  1216. }
  1217. }
  1218. }
  1219. if (IS_AJAX_POST) {
  1220. if (empty($verify)) {
  1221. $this->success('没有核销商品,直接进入购买');
  1222. } else if (empty($delivery)) {
  1223. $this->success('没有实体商品,直接进入购买');
  1224. } else if ($verify === $delivery) {
  1225. $this->success('数据相等');
  1226. } else {
  1227. $this->error('数据不相等');
  1228. }
  1229. } else {
  1230. $result = [
  1231. 0 => [
  1232. 'amount' => 0,
  1233. 'number' => 0,
  1234. 'cart_ids' => '',
  1235. 'list' => $delivery,
  1236. 'name' => '快递发货',
  1237. ],
  1238. 1 => [
  1239. 'amount' => 0,
  1240. 'number' => 0,
  1241. 'cart_ids' => '',
  1242. 'list' => $verify,
  1243. 'name' => '到店核销',
  1244. ],
  1245. ];
  1246. foreach ($delivery as $key => $value) {
  1247. // 合计数量
  1248. $result[0]['number'] += $value['product_num'];
  1249. // 合计金额
  1250. $result[0]['amount'] += $value['subtotal'];
  1251. $result[0]['amount'] = unifyPriceHandle($result[0]['amount']);
  1252. // 购物车ID
  1253. $result[0]['cart_ids'] .= !empty($key) ? ',' . $value['cart_id'] : $value['cart_id'];
  1254. }
  1255. $result[0]['submitBuyGoods'] = ' data-ids="'.$result[0]['cart_ids'].'" onclick="submitBuyGoods(this);" ';
  1256. foreach ($verify as $key => $value) {
  1257. // 合计数量
  1258. $result[1]['number'] += $value['product_num'];
  1259. // 合计金额
  1260. $result[1]['amount'] += $value['subtotal'];
  1261. $result[1]['amount'] = unifyPriceHandle($result[1]['amount']);
  1262. // 购物车ID
  1263. $result[1]['cart_ids'] .= !empty($key) ? ',' . $value['cart_id'] : $value['cart_id'];
  1264. }
  1265. $result[1]['submitBuyGoods'] = ' data-ids="'.$result[1]['cart_ids'].'" onclick="submitBuyGoods(this);" ';
  1266. $this->assign('result', $result);
  1267. return $this->fetch('users/shop_to_split_goods');
  1268. }
  1269. }
  1270. public function submitBuyGoods()
  1271. {
  1272. if (IS_AJAX_POST) {
  1273. $ids = input('post.ids', '');
  1274. if (!empty($ids)) {
  1275. $where = [
  1276. 'cart_id' => ['NOT IN', $ids],
  1277. 'users_id' => $this->users_id,
  1278. ];
  1279. $update = [
  1280. 'selected' => 0,
  1281. 'update_time' => getTime()
  1282. ];
  1283. Db::name("shop_cart")->where($where)->update($update);
  1284. $this->success('数据正确', url('user/Shop/shop_under_order'));
  1285. }
  1286. $this->error('数据错误');
  1287. }
  1288. }
  1289. public function select_verify_store()
  1290. {
  1291. // 查询条件
  1292. $where = [
  1293. 'store_status' => 1,
  1294. ];
  1295. $list = Db::name("weapp_verify_store")->where($where)->order('store_id desc')->select();
  1296. $regionList = get_region_list();
  1297. foreach ($list as $key => $value) {
  1298. // 省市县区地址处理
  1299. $city = !empty($regionList[$value['store_city']]['name']) ? $regionList[$value['store_city']]['name'] : '';
  1300. $area = !empty($regionList[$value['store_area']]['name']) ? $regionList[$value['store_area']]['name'] : '';
  1301. $prov = !empty($regionList[$value['store_province']]['name']) ? $regionList[$value['store_province']]['name'] : '';
  1302. $value['prov_city_area'] = $prov . ' ' . $city . ' ' . $area;
  1303. $list[$key] = $value;
  1304. }
  1305. $this->assign('list', $list);
  1306. return $this->fetch('users/shop_select_verify_store');
  1307. }
  1308. // 添加收货地址
  1309. public function shop_add_address()
  1310. {
  1311. if (IS_AJAX_POST) {
  1312. $post = input('post.');
  1313. if (empty($post['consignee'])) $this->error('请填写收货人姓名');
  1314. if (empty($post['mobile'])) $this->error('请填写收货人手机');
  1315. if (empty($post['province'])) $this->error('请完善收货地址');
  1316. if (empty($post['address'])) $this->error('请填写详细地址');
  1317. // 添加数据
  1318. $post['users_id'] = $this->users_id;
  1319. $post['add_time'] = getTime();
  1320. $post['lang'] = $this->home_lang;
  1321. if (isMobile() && isWeixin()) {
  1322. // 在手机微信端、小程序中则把新增的收货地址设置为默认地址
  1323. if ('v3' == $this->usersTplVersion) {
  1324. $post['is_default'] = !empty($post['is_default']) ? 1 : 0;
  1325. } else {
  1326. $post['is_default'] = 1;// 设置为默认地址
  1327. }
  1328. } else {
  1329. $post['is_default'] = !empty($post['is_default']) ? 1 : 0;
  1330. }
  1331. if (isset($post['is_default'])) $post['is_default'] = !empty($post['is_default']) ? 1 : 0;
  1332. $addr_id = $this->shop_address_db->insertGetId($post);
  1333. if (!empty($addr_id)) {
  1334. // 把对应会员下的所有地址改为非默认
  1335. if (!empty($post['is_default'])) {
  1336. $where = [
  1337. 'addr_id' => array('NEQ', $addr_id),
  1338. 'users_id' => $this->users_id,
  1339. 'lang' => $this->home_lang,
  1340. ];
  1341. $update = [
  1342. 'is_default' => 1,
  1343. 'update_time' => getTime()
  1344. ];
  1345. $this->shop_address_db->where($where)->update($update);
  1346. }
  1347. // 如果是移动端(手机端、微信端)并且有v2x参数则执行
  1348. if ((isMobile() || isWeixin()) && !empty($post['v2x'])) {
  1349. $url = session($this->users_id.'_EyouShopOrderUrl');
  1350. $this->success('添加成功', $url, ['url' => $url]);
  1351. }
  1352. if (!empty($post['type']) && 'list' == $post['type']) {
  1353. $url = url('user/Shop/shop_address_list');
  1354. $this->success('添加成功', $url, ['url' => $url]);
  1355. }
  1356. // 根据地址ID查询相应的中文名字
  1357. $post['country'] = '中国';
  1358. $post['province'] = get_province_name($post['province']);
  1359. $post['city'] = get_city_name($post['city']);
  1360. $post['district'] = get_area_name($post['district']);
  1361. $post['addr_id'] = $addr_id;
  1362. $post['is_mobile'] = $this->is_mobile;
  1363. $post['Info'] = $post['province'].' '.$post['city'].' '.$post['district'];
  1364. $this->success('添加成功!', null, $post);
  1365. } else {
  1366. $this->error('数据有误!');
  1367. }
  1368. }
  1369. $types = input('param.type');
  1370. if ('list' == $types || 'order' == $types || 'order_new' == $types) {
  1371. $Where = [
  1372. 'users_id' => $this->users_id,
  1373. 'lang' => $this->home_lang,
  1374. ];
  1375. $addr_num = $this->shop_address_db->where($Where)->count();
  1376. $eyou = [
  1377. 'field' => [
  1378. 'Province' => get_province_list(),
  1379. 'types' => $types,
  1380. 'addr_num' => $addr_num,
  1381. ],
  1382. ];
  1383. $this->assign('eyou',$eyou);
  1384. }else{
  1385. $this->error('非法来源!');
  1386. }
  1387. $this->assign('is_mobile', $this->is_mobile);
  1388. $this->assign('add_addr_url', url("user/Shop/shop_get_wechat_addr"));
  1389. // 将当前URL存入 session ,使用微信获取收货地址时需要验签加密
  1390. session($this->users_id.'_EyouShopAddAddress', $this->request->url(true));
  1391. return $this->fetch('users/shop_add_address');
  1392. }
  1393. // 更新收货地址
  1394. public function shop_edit_address()
  1395. {
  1396. if (IS_AJAX_POST) {
  1397. $post = input('post.');
  1398. if (empty($post['consignee'])) $this->error('请填写收货人姓名');
  1399. if (empty($post['mobile'])) $this->error('请填写收货人手机');
  1400. if (empty($post['province'])) $this->error('请完善收货地址');
  1401. if (empty($post['address'])) $this->error('请填写详细地址');
  1402. // 更新条件及数据
  1403. $post['addr_id'] = intval($post['addr_id']);
  1404. $post['users_id'] = $this->users_id;
  1405. $post['update_time'] = getTime();
  1406. $post['lang'] = $this->home_lang;
  1407. $post['country'] = (empty($post['country']) || $post['country'] == '中国') ? 0 : 1;
  1408. if (isset($post['is_default'])) $post['is_default'] = !empty($post['is_default']) ? 1 : 0;
  1409. $where = [
  1410. 'addr_id' => $post['addr_id'],
  1411. 'users_id' => $this->users_id,
  1412. 'lang' => $this->home_lang,
  1413. ];
  1414. $addr_id = $this->shop_address_db->where($where)->update($post);
  1415. if (!empty($addr_id)) {
  1416. // 把对应会员下的所有地址改为非默认
  1417. if (isset($post['is_default']) && 1 == $post['is_default']) {
  1418. $AddressWhere = [
  1419. 'addr_id' => array('NEQ',$post['addr_id']),
  1420. 'users_id' => $this->users_id,
  1421. 'lang' => $this->home_lang,
  1422. ];
  1423. $data_new['is_default'] = 0;// 设置为非默认地址
  1424. $data_new['update_time'] = getTime();
  1425. $this->shop_address_db->where($AddressWhere)->update($data_new);
  1426. }
  1427. // 如果是移动端(手机端、微信端)并且有v2x参数则执行
  1428. if ((isMobile() || isWeixin()) && !empty($post['v2x'])) {
  1429. $url = session($this->users_id.'_EyouShopOrderUrl');
  1430. $this->success('修改成功', $url, ['url' => $url]);
  1431. }
  1432. // 根据地址ID查询相应的中文名字
  1433. $post['country'] = '中国';
  1434. $post['province'] = get_province_name($post['province']);
  1435. $post['city'] = get_city_name($post['city']);
  1436. $post['district'] = get_area_name($post['district']);
  1437. $this->success('修改成功', null, $post);
  1438. } else {
  1439. $this->error('数据有误!');
  1440. }
  1441. }
  1442. // 查询收货地址
  1443. $addr_id = input('param.addr_id/d', 0);
  1444. $where = [
  1445. 'addr_id' => $addr_id,
  1446. 'users_id' => $this->users_id,
  1447. 'lang' => $this->home_lang,
  1448. ];
  1449. $address = $this->shop_address_db->where($where)->find();
  1450. if (empty($address)) $this->error('数据有误!');
  1451. // 处理收货地址
  1452. if (in_array($this->usersTplVersion, ['v3']) || in_array($this->usersTpl2xVersion, ['v2.x'])) {
  1453. $address['province_name'] = get_province_name($address['province']);
  1454. $address['city_name'] = get_city_name($address['city']);
  1455. $address['district_name'] = get_area_name($address['district']);
  1456. }
  1457. $address['country'] = '中国'; //国家
  1458. $address['Province'] = get_province_list(); // 省份
  1459. $address['City'] = $this->region_db->where('parent_id',$address['province'])->select(); // 城市
  1460. $address['District'] = $this->region_db->where('parent_id',$address['city'])->select(); // 县/区/镇
  1461. $address['onDelAddress'] = " onclick=\"DelAddress('{$addr_id}', this);\" ";
  1462. $type = input('param.type/s', 'list');
  1463. $gourl = input('param.gourl/s', '', 'urldecode');
  1464. $delAddressUrl = url('Shop/shop_del_address', ['type'=>$type, 'gourl'=>$gourl]);
  1465. $address['delAddressUrl'] = $delAddressUrl;
  1466. $eyou = [
  1467. 'field' => $address,
  1468. ];
  1469. $this->assign('eyou', $eyou);
  1470. return $this->fetch('users/shop_edit_address');
  1471. }
  1472. // 删除收货地址
  1473. public function shop_del_address()
  1474. {
  1475. if (IS_POST) {
  1476. $addr_id = input('post.addr_id/d');
  1477. $Where = [
  1478. 'addr_id' => $addr_id,
  1479. 'users_id' => $this->users_id,
  1480. 'lang' => $this->home_lang,
  1481. ];
  1482. $return = $this->shop_address_db->where($Where)->delete();
  1483. if ($return) {
  1484. $gourl = '';
  1485. $type = input('param.type/s', 'list');
  1486. if ('order' == $type) {
  1487. $gourl = input('param.gourl/s', '', 'urldecode');
  1488. }
  1489. $url = url('Shop/shop_address_list', ['type'=>$type, 'gourl'=>$gourl]);
  1490. $this->success('删除成功!', null, ['url'=>$url]);
  1491. }else{
  1492. $this->error('删除失败!');
  1493. }
  1494. }
  1495. }
  1496. // 更新收货地址,设置为默认地址
  1497. public function shop_set_default_address()
  1498. {
  1499. if (IS_AJAX_POST) {
  1500. $post = input('post.');
  1501. // 更新条件及数据
  1502. $post['addr_id'] = intval($post['addr_id']);
  1503. $post['users_id'] = $this->users_id;
  1504. $post['is_default'] = '1'; //设置为默认
  1505. $post['add_time'] = getTime();
  1506. $post['lang'] = $this->home_lang;
  1507. $AddrWhere = [
  1508. 'addr_id' => $post['addr_id'],
  1509. 'users_id' => $this->users_id,
  1510. 'lang' => $this->home_lang,
  1511. ];
  1512. $addr_id = $this->shop_address_db->where($AddrWhere)->update($post);
  1513. if (!empty($addr_id)) {
  1514. // 把对应会员下的所有地址改为非默认
  1515. $AddressWhere = [
  1516. 'addr_id' => array('NEQ',$post['addr_id']),
  1517. 'users_id' => $this->users_id,
  1518. 'lang' => $this->home_lang,
  1519. ];
  1520. $data['is_default'] = '0';// 设置为非默认
  1521. $data['update_time'] = getTime();
  1522. $this->shop_address_db->where($AddressWhere)->update($data);
  1523. $this->success('设置成功!');
  1524. }else{
  1525. $this->error('数据有误!');
  1526. }
  1527. }
  1528. }
  1529. // 查询运费
  1530. public function shop_inquiry_shipping()
  1531. {
  1532. if (IS_AJAX_POST) {
  1533. $shop_open_shipping = getUsersConfigData('shop.shop_open_shipping');
  1534. if (empty($shop_open_shipping)) $this->success('未开启运费', '', 0);
  1535. // 查询会员收货地址,获取省份
  1536. $addr_id = input('post.addr_id');
  1537. if ($this->usersTplVersion == 'v3') { // 第三套小米的会员中心
  1538. if (empty($addr_id)) {
  1539. $addr_id = Db::name('shop_address')->where(['users_id'=>$this->users_id])->order('addr_id asc')->value('addr_id');
  1540. }
  1541. }
  1542. $where = [
  1543. 'addr_id' => $addr_id,
  1544. 'users_id' => $this->users_id,
  1545. 'lang' => $this->home_lang,
  1546. ];
  1547. $province = $this->shop_address_db->where($where)->getField('province');
  1548. // 通过省份获取运费模板中的运费价格
  1549. $template_money = $this->shipping_template_db->where('province_id', $province)->getField('template_money');
  1550. if (0 == $template_money) {
  1551. // 省份运费价格为0时,使用统一的运费价格,固定ID为100000
  1552. $template_money = $this->shipping_template_db->where('province_id', '100000')->getField('template_money');
  1553. }
  1554. // 查询下单商品信息
  1555. $querystr = input('post.querystr/s', '');
  1556. $merchantID = 0;
  1557. if (!empty($querystr)) {
  1558. // 解析参数查询商品信息
  1559. $querystr = cookie($querystr);
  1560. $aid = !empty($querystr['aid']) ? $querystr['aid'] : 0;
  1561. $where = [
  1562. 'aid' => $aid
  1563. ];
  1564. $archives = $this->archives_db->field('merchant_id, free_shipping')->where($where)->select();
  1565. $merchantID = count(array_unique(get_arr_column($archives, 'merchant_id')));
  1566. } else {
  1567. $where = [
  1568. 'a.selected' => 1,
  1569. 'a.users_id' => $this->users_id,
  1570. ];
  1571. $field = 'b.merchant_id, b.free_shipping';
  1572. $archives = $this->shop_cart_db->alias('a')->field($field)->join('__ARCHIVES__ b', 'a.product_id = b.aid', 'LEFT')->where($where)->select();
  1573. $merchantID = count(array_unique(get_arr_column($archives, 'merchant_id')));
  1574. }
  1575. if (intval($merchantID) >= 1) {
  1576. // 处理查询出需要运费的商品数量
  1577. $merchantIDArr = [];
  1578. $shippingFeeNum = 0;
  1579. foreach ($archives as $key => $value) {
  1580. if (!in_array($value['merchant_id'], $merchantIDArr)) {
  1581. if (isset($value['free_shipping']) && 0 === intval($value['free_shipping'])) {
  1582. $shippingFeeNum++;
  1583. array_push($merchantIDArr, $value['merchant_id']);
  1584. }
  1585. }
  1586. }
  1587. $template_money = strval($template_money) * strval($shippingFeeNum);
  1588. }
  1589. $template_money = floatval($template_money);
  1590. $this->success('查询成功!', '', $template_money);
  1591. } else {
  1592. $this->error('订单号错误');
  1593. }
  1594. }
  1595. // 联动地址获取
  1596. public function get_region_data()
  1597. {
  1598. $parent_id = input('param.parent_id/d', 0);
  1599. $regionData = $this->region_db->where("parent_id", $parent_id)->select();
  1600. if (in_array($this->usersTpl2xVersion, ['v2.x']) || in_array($this->usersTplVersion, ['v3'])) {
  1601. $this->success('查询成功!', null, $regionData);
  1602. } else {
  1603. $html = '';
  1604. if (!empty($regionData)) {
  1605. // 拼装下拉选项
  1606. foreach($regionData as $value) {
  1607. $html .= "<option value='{$value['id']}'>{$value['name']}</option>";
  1608. }
  1609. }
  1610. echo json_encode($html);
  1611. }
  1612. }
  1613. // 会员提醒收货
  1614. public function shop_order_remind()
  1615. {
  1616. if (IS_AJAX_POST) {
  1617. $post = input('post.');
  1618. // 添加订单操作记录
  1619. AddOrderAction($post['order_id'],$this->users_id,'0','1','0','1','提醒成功!','会员提醒管理员及时发货!');
  1620. $this->success('提醒成功!');
  1621. }else{
  1622. $this->error('订单号错误');
  1623. }
  1624. }
  1625. // 会员确认收货
  1626. public function shop_member_confirm()
  1627. {
  1628. if (IS_AJAX_POST) {
  1629. $post = input('post.');
  1630. $post['order_id'] = intval($post['order_id']);
  1631. // 更新条件
  1632. $where = [
  1633. 'order_id' => $post['order_id'],
  1634. 'users_id' => $this->users_id,
  1635. 'lang' => $this->home_lang,
  1636. ];
  1637. // 更新数据
  1638. $update = [
  1639. 'order_status' => 3,
  1640. 'confirm_time' => getTime(),
  1641. 'update_time' => getTime(),
  1642. // 如果后台【商城中心】-【商城配置】-【订单设置】-收货后可维权时间设置为0,则表示订单不允许申请维权
  1643. 'allow_service' => !empty($this->usersConfig['order_right_protect_time']) ? 0 : 1
  1644. ];
  1645. // 更新订单主表
  1646. $return = $this->shop_order_db->where($where)->update($update);
  1647. if (!empty($return)) {
  1648. // 查询订单是否存在多商家,存在多商家则执行
  1649. $shopOrder = $this->shop_order_db->where($where)->find();
  1650. if (!empty($shopOrder['merchant_id'])) {
  1651. // 处理多商家订单金额结算
  1652. $multiMerchantLogic = new \app\user\logic\MultiMerchantLogic;
  1653. $multiMerchantLogic->handleMultiMerchantOrderSettle($shopOrder);
  1654. }
  1655. // 更新数据
  1656. $update = [
  1657. 'update_time' => getTime(),
  1658. ];
  1659. // 更新订单明细表
  1660. $this->shop_order_details_db->where($where)->update($update);
  1661. // 添加订单操作记录
  1662. AddOrderAction($post['order_id'], $this->users_id, 0, 3, 1, 1, '确认收货', '会员已确认收到货物,订单完成');
  1663. // 如果安装了分销插件则执行
  1664. if (is_dir('./weapp/DealerPlugin/')) {
  1665. // 开启分销插件则执行
  1666. $weappInfo = model('Weapp')->getWeappList('DealerPlugin');
  1667. if (!empty($weappInfo['status']) && 1 == $weappInfo['status']) {
  1668. // 调用分销逻辑层方法
  1669. $dealerCommonLogic = new \weapp\DealerPlugin\logic\DealerCommonLogic;
  1670. $dealerCommonLogic->dealerOrderSettlementExecute($post['order_id'], $this->users_id);
  1671. }
  1672. }
  1673. $this->success('会员确认收货');
  1674. }else{
  1675. $this->error('订单号错误');
  1676. }
  1677. }
  1678. }
  1679. // 获取微信收货地址
  1680. public function shop_get_wechat_addr()
  1681. {
  1682. if (IS_AJAX_POST) {
  1683. // 微信配置信息
  1684. $WeChatLoginConfig = !empty($this->usersConfig['wechat_login_config']) ? unserialize($this->usersConfig['wechat_login_config']) : [];
  1685. $appid = !empty($WeChatLoginConfig['appid']) ? $WeChatLoginConfig['appid'] : '';
  1686. $appsecret = !empty($WeChatLoginConfig['appsecret']) ? $WeChatLoginConfig['appsecret'] : '';
  1687. if (empty($appid)) {
  1688. $this->error('后台微信配置尚未配置AppId,不可以获取微信地址!');
  1689. }else if (empty($appsecret)) {
  1690. $this->error('后台微信配置尚未配置AppSecret,不可以获取微信地址!');
  1691. }
  1692. // 当前时间戳
  1693. $time = getTime();
  1694. // 微信access_token和jsapi_ticket信息
  1695. $WechatData = getUsersConfigData('wechat');
  1696. // access_token信息判断
  1697. $accesstoken = !empty($WechatData['wechat_token_value']) ? $WechatData['wechat_token_value'] : [];
  1698. if (empty($accesstoken)) {
  1699. // 如果配置表中的accesstoken为空则执行
  1700. // 获取公众号access_token,接口限制10万次/天
  1701. $return = $this->shop_model->GetWeChatAccessToken($appid,$appsecret);
  1702. if (empty($return['status'])) {
  1703. $this->error($return['prompt']);
  1704. }else{
  1705. $accesstoken = $return['token'];
  1706. }
  1707. }else if ($time > ($WechatData['wechat_token_time']+7000)) {
  1708. // 如果配置表中的时间超过过期时间则执行
  1709. // 获取公众号access_token,接口限制10万次/天
  1710. $return = $this->shop_model->GetWeChatAccessToken($appid,$appsecret);
  1711. if (empty($return['status'])) {
  1712. $this->error($return['prompt']);
  1713. }else{
  1714. $accesstoken = $return['token'];
  1715. }
  1716. }
  1717. // jsapi_ticket信息判断
  1718. $jsapi_ticket = !empty($WechatData['wechat_ticket_value']) ? $WechatData['wechat_ticket_value'] : [];
  1719. if (empty($jsapi_ticket)) {
  1720. // 获取公众号jsapi_ticket,接口限制500万次/天
  1721. $return = $this->shop_model->GetWeChatJsapiTicket($accesstoken);
  1722. if (empty($return['status'])) {
  1723. $this->error($return['prompt']);
  1724. }else{
  1725. $jsapi_ticket = $return['ticket'];
  1726. }
  1727. }else if ($time > ($WechatData['wechat_ticket_time']+7000)) {
  1728. // 获取公众号jsapi_ticket,接口限制500万次/天
  1729. $return = $this->shop_model->GetWeChatJsapiTicket($accesstoken);
  1730. if (empty($return['status'])) {
  1731. $this->error($return['prompt']);
  1732. }else{
  1733. $jsapi_ticket = $return['ticket'];
  1734. }
  1735. }
  1736. // <---- 加密参数开始
  1737. // 微信公众号jsapi_ticket
  1738. // $jsapi_ticket = $jsapi_ticket;
  1739. // 随机字符串
  1740. $noncestr = $this->shop_model->GetRandomString('16');
  1741. $noncestr = "$noncestr";
  1742. // 当前时间戳
  1743. $timestamp = time();
  1744. $timestamp = "$timestamp";
  1745. // 当前访问接口URL
  1746. if ($this->usersTplVersion == 'v3') {
  1747. $url = session($this->users_id . '_EyouShopAddAddress');
  1748. } else {
  1749. $url = $this->request->url(true);
  1750. }
  1751. // 加密参数结束 ----->
  1752. // 参数加密,顺序固定不可改变
  1753. $string = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
  1754. $signature = SHA1($string);
  1755. // 返回结果
  1756. $result = [
  1757. // 用于调试,不影响正常业务(如不需要,可直接清理)
  1758. 'token' => $accesstoken,
  1759. 'ticket' => $jsapi_ticket,
  1760. 'url' => $url, // 传入接口调用参数(必须返回)
  1761. 'appid' => $appid,
  1762. 'timestamp' => $timestamp,
  1763. 'noncestr' => $noncestr,
  1764. 'signature' => $signature,
  1765. ];
  1766. $this->success('数据获取!',null,$result);
  1767. }
  1768. $result = [
  1769. 'wechat_url' => url("user/Shop/shop_get_wechat_addr"),
  1770. 'add_addr_url' => url("user/Shop/shop_add_address"),
  1771. ];
  1772. $eyou = array(
  1773. 'field' => $result,
  1774. );
  1775. $this->assign('eyou', $eyou);
  1776. return $this->fetch('users/shop_get_wechat_addr');
  1777. }
  1778. // 添加微信的收货地址到数据库
  1779. public function add_wechat_addr()
  1780. {
  1781. if (IS_AJAX_POST) {
  1782. $post = input('post.');
  1783. // 省
  1784. $province = $this->region_db->where('name',$post['provinceName'])->getField('id');
  1785. // 市
  1786. $city = $this->region_db->where('name',$post['cityName'])->getField('id');
  1787. // 县
  1788. $district = $this->region_db->where('name',$post['countryName'])->getField('id');
  1789. // 查询这个收货地址是否存在
  1790. $where = [
  1791. 'users_id' => $this->users_id,
  1792. 'consignee' => $post['userName'],
  1793. 'mobile' => $post['telNumber'],
  1794. 'province' => $province,
  1795. 'city' => $city,
  1796. 'district' => $district,
  1797. 'address' => $post['detailInfo'],
  1798. 'lang' => $this->home_lang,
  1799. ];
  1800. $return = $this->shop_address_db->where($where)->find();
  1801. if (!empty($return)) {
  1802. $this->success('获取成功!',session($this->users_id.'_EyouShopOrderUrl'));
  1803. }else{
  1804. $data = [
  1805. 'users_id' => $this->users_id,
  1806. 'consignee' => $post['userName'],
  1807. 'mobile' => $post['telNumber'],
  1808. 'province' => $province,
  1809. 'city' => $city,
  1810. 'district' => $district,
  1811. 'address' => $post['detailInfo'],
  1812. 'is_default' => 1, // 设置为默认地址
  1813. 'lang' => $this->home_lang,
  1814. 'add_time' => getTime(),
  1815. ];
  1816. if ($this->usersTplVersion == 'v3') $data['is_default'] = 0;
  1817. $addr_id = $this->shop_address_db->add($data);
  1818. if (!empty($addr_id)) {
  1819. if ($this->usersTplVersion != 'v3') {
  1820. // 把对应会员下的所有地址改为非默认
  1821. $AddressWhere = [
  1822. 'addr_id' => array('NEQ',$addr_id),
  1823. 'users_id' => $this->users_id,
  1824. 'lang' => $this->home_lang,
  1825. ];
  1826. $data_new['is_default'] = '0';// 设置为非默认地址
  1827. $data_new['update_time'] = getTime();
  1828. $this->shop_address_db->where($AddressWhere)->update($data_new);
  1829. }
  1830. $this->success('获取成功!',session($this->users_id.'_EyouShopOrderUrl'));
  1831. }else{
  1832. $this->success('获取失败,请刷新后重试!');
  1833. }
  1834. }
  1835. }
  1836. }
  1837. // 判断商品是否库存为0
  1838. private function IsSoldOut($param = array())
  1839. {
  1840. if (!empty($param['aid'])) {
  1841. if (!empty($param['spec_value_id'])) {
  1842. $SpecWhere = [
  1843. 'aid' => $param['aid'],
  1844. 'lang' => $this->home_lang,
  1845. 'spec_stock' => ['>',0],
  1846. 'spec_value_id' => $param['spec_value_id'],
  1847. ];
  1848. $spec_stock = Db::name('product_spec_value')->where($SpecWhere)->getField('spec_stock');
  1849. if (empty($spec_stock)) {
  1850. $data['code'] = -1; // 已售罄
  1851. $this->error('商品已售罄!', null, $data);
  1852. }
  1853. if ($spec_stock < $param['num']) {
  1854. $data['code'] = -1; // 库存不足
  1855. $this->error('商品最大库存:'.$spec_stock, null, $data);
  1856. }
  1857. } else {
  1858. $archives_where = [
  1859. 'arcrank' => array('egt','0'), //带审核稿件不查询
  1860. 'aid' => $param['aid'],
  1861. 'lang' => $this->home_lang,
  1862. ];
  1863. $stock_count = $this->archives_db->where($archives_where)->getField('stock_count');
  1864. if (empty($stock_count)) {
  1865. $data['code'] = -1; // 已售罄
  1866. $this->error('商品已售罄!', null, $data);
  1867. }
  1868. if ($stock_count < $param['num']) {
  1869. $data['code'] = -1; // 库存不足
  1870. $this->error('商品最大库存:'.$stock_count, null, $data);
  1871. }
  1872. }
  1873. }
  1874. }
  1875. // 申请退换货服务中转页
  1876. public function service_list()
  1877. {
  1878. $order_id = input('param.order_id/d', 0);
  1879. if (empty($order_id)) $this->error('订单不存在!');
  1880. // 查询订单商品
  1881. $where = [
  1882. 'order_id' => $order_id,
  1883. 'users_id' => $this->users_id,
  1884. ];
  1885. $order_details = $this->shop_order_details_db->where($where)->select();
  1886. if (empty($order_details)) $this->error('订单不存在!');
  1887. // 查询订单商品售后
  1888. $order_service = $this->shop_order_service_db->where($where)->getAllWithIndex('details_id');
  1889. foreach ($order_details as $k => $v) {
  1890. // 图片链接
  1891. $v['litpic'] = !empty($v['litpic']) ? handle_subdir_pic($v['litpic']) : '';
  1892. if (!empty($v['apply_service']) && !empty($order_service[$v['details_id']]['service_id'])) {
  1893. // 售后链接
  1894. $v['name'] = '查看售后';
  1895. $v['url'] = url('user/Shop/after_service_details', ['service_id'=>$order_service[$v['details_id']]['service_id']]);
  1896. } else {
  1897. // 申请链接
  1898. $v['name'] = '申请售后';
  1899. $v['url'] = url('user/Shop/after_service_apply', ['details_id'=>$v['details_id']]);
  1900. }
  1901. // 获取订单商品规格列表(购买时的商品规格)
  1902. $v['product_spec_list'] = model('ShopPublicHandle')->getOrderGoodsSpecList($v);
  1903. // 覆盖原数据
  1904. $order_details[$k] = $v;
  1905. }
  1906. $eyou = [
  1907. 'field' => $order_details
  1908. ];
  1909. $this->assign('eyou', $eyou);
  1910. return $this->fetch('users/shop_after_service_list');
  1911. }
  1912. /*------陈风任---2021-1-12---售后服务(退换货)------开始------*/
  1913. // 申请退换货服务
  1914. public function after_service_apply()
  1915. {
  1916. if (IS_AJAX_POST) {
  1917. $post = input('post.');
  1918. if (empty($post)) $this->error('提交数据有误!');
  1919. if (empty($post['service_type'])) $this->error('请选择服务类型!');
  1920. if (empty($post['content'])) $this->error('请填写问题描述!');
  1921. if (empty($post['address'])) $this->error('请填写您的收货地址!');
  1922. if (empty($post['consignee'])) $this->error('请填写您的姓名!');
  1923. if (empty($post['mobile'])) $this->error('请填写您的手机号码!');
  1924. $time = getTime();
  1925. $post['order_id'] = intval($post['order_id']);
  1926. $post['details_id'] = intval($post['details_id']);
  1927. $post['product_id'] = intval($post['product_id']);
  1928. $post['product_num'] = intval($post['product_num']);
  1929. $post['service_type'] = intval($post['service_type']);
  1930. $post['refund_price'] = unifyPriceHandle($post['refund_price']);
  1931. // 查询订单详情信息
  1932. $details = $this->shop_model->GetOrderDetailsInfo($post['details_id'], $this->users_id);
  1933. if (isset($details['code']) && 0 === intval($details['code'])) $this->error($details['msg']);
  1934. // 组合添加数据
  1935. $data = [
  1936. 'users_id' => $this->users_id,
  1937. 'merchant_id' => $details['merchant_id'],
  1938. 'address' => $post['addrinfo'].' '.$post['address'],
  1939. 'upload_img' => !empty($post['upload_img']) ? implode(',', $post['upload_img']) : '',
  1940. 'refund_balance' => '',
  1941. 'refund_point' => '',
  1942. 'refund_code' => 'HH' . $time . rand(10,99),
  1943. 'add_time' => $time,
  1944. 'update_time' => $time,
  1945. ];
  1946. if (2 === intval($post['service_type']) && !empty($post['refund_price'])) $data['refund_code'] = 'TK' . $time . rand(10,99);
  1947. $insert = array_merge($post, $data);
  1948. $resultID = $this->shop_order_service_db->insertGetId($insert);
  1949. if (!empty($resultID)) {
  1950. // 更新订单明细表中对应商品为申请服务
  1951. $update = [
  1952. 'apply_service' => 1,
  1953. 'update_time' => getTime()
  1954. ];
  1955. $this->shop_order_details_db->where('details_id', $post['details_id'])->update($update);
  1956. // 添加订单服务记录
  1957. $LogNote = 1 == $post['service_type'] ? '用户提交换货申请,待审核!' : '用户提交退货申请,待审核!';
  1958. OrderServiceLog($resultID, $post['order_id'], $this->users_id, 0, $LogNote);
  1959. // 返回结束
  1960. $this->success('已申请,待审核', url('user/Shop/after_service_details', ['service_id' => $resultID]));
  1961. } else {
  1962. $this->error('申请失败!');
  1963. }
  1964. }
  1965. $details_id = input('param.details_id/d');
  1966. if (empty($details_id)) $this->error('售后服务单不存在!');
  1967. // 查询订单中单个商品信息
  1968. $data = $this->shop_model->GetOrderDetailsInfo($details_id, $this->users_id);
  1969. // 返回错误提示
  1970. if (!empty($data['msg']) && isset($data['code']) && 0 == $data['code']) $this->error($data['msg']);
  1971. // 商户收货信息
  1972. $maddr = getUsersConfigData('addr');
  1973. // 如果存在商家ID则执行
  1974. if (!empty($data['merchant_id'])) {
  1975. // 查询商家地址信息
  1976. $multiMerchantLogic = new \app\user\logic\MultiMerchantLogic;
  1977. $maddr = $multiMerchantLogic->getMultiMerchantContact($data['merchant_id']);
  1978. }
  1979. $eyou = [
  1980. 'url' => url('user/Shop/after_service_apply', ['_ajax'=>1]),
  1981. 'maddr' => $maddr,
  1982. 'field' => $data
  1983. ];
  1984. $this->assign('eyou', $eyou);
  1985. return $this->fetch('users/shop_after_service_apply');
  1986. }
  1987. // 退货换服务列表
  1988. public function after_service()
  1989. {
  1990. $order_code = input('param.order_code');
  1991. $status = input('param.status/s');
  1992. $keywords = input('param.keywords/s');
  1993. $ServiceInfo = $this->shop_model->GetAllServiceInfo($this->users_id, $order_code, $status, $keywords);
  1994. $where = [
  1995. 'lang' => $this->home_lang,
  1996. 'users_id' => $this->users_id
  1997. ];
  1998. //处理中
  1999. $where['status'] = ['in',[1,2,3,4,5]];
  2000. $ServicingTotal = $this->shop_order_service_db->where($where)->count();
  2001. //已完成
  2002. $where['status'] = ['in',[6,7,8]];
  2003. $ServicedTotal = $this->shop_order_service_db->where($where)->count();
  2004. $eyou = [
  2005. 'field' => [
  2006. 'service' => $ServiceInfo['Service'],
  2007. 'pageStr' => $ServiceInfo['pageStr'],
  2008. 'ServicingTotal' => $ServicingTotal,
  2009. 'ServicedTotal' => $ServicedTotal,
  2010. ],
  2011. ];
  2012. $this->assign('eyou', $eyou);
  2013. return $this->fetch('users/shop_after_service');
  2014. }
  2015. // 申请服务详情
  2016. public function after_service_details()
  2017. {
  2018. /*取消服务单*/
  2019. if (IS_AJAX_POST) {
  2020. $param = input('param.');
  2021. if (empty($param['service_id'])) $this->error('请选择需要取消的服务单!');
  2022. $param['service_id'] = intval($param['service_id']);
  2023. $param['details_id'] = intval($param['details_id']);
  2024. /*取消服务单*/
  2025. $where = [
  2026. 'users_id' => $this->users_id,
  2027. 'service_id' => $param['service_id'],
  2028. ];
  2029. $update = [
  2030. 'status' => 8,
  2031. 'update_time' => getTime(),
  2032. ];
  2033. $ResultID = $this->shop_order_service_db->where($where)->update($update);
  2034. /* END */
  2035. if (!empty($ResultID)) {
  2036. /*更新订单明细表中对应商品为未申请服务*/
  2037. $where = [
  2038. 'users_id' => $this->users_id,
  2039. 'details_id' => $param['details_id']
  2040. ];
  2041. $update = [
  2042. 'apply_service' => 0,
  2043. 'update_time' => getTime()
  2044. ];
  2045. $this->shop_order_details_db->where($where)->update($update);
  2046. /* END */
  2047. /*添加记录单*/
  2048. $param['users_id'] = $this->users_id;
  2049. $param['status'] = 8;
  2050. $this->shop_common->AddOrderServiceLog($param, 1);
  2051. /* END */
  2052. $this->success('取消成功!');
  2053. } else {
  2054. $this->error('取消失败!');
  2055. }
  2056. }
  2057. /* END */
  2058. // 查询售后单详情
  2059. $service_id = input('param.service_id/d');
  2060. if (empty($service_id)) {
  2061. $url = url('user/Shop/shop_centre');
  2062. $this->error('售后服务单不存在!', $url);
  2063. }
  2064. $details = $this->shop_model->GetServiceDetailsInfo($service_id, $this->users_id);
  2065. // 商户收货信息
  2066. $maddr = getUsersConfigData('addr');
  2067. // 如果存在商家ID则执行
  2068. if (!empty($details['merchant_id'])) {
  2069. // 查询商家地址信息
  2070. $multiMerchantLogic = new \app\user\logic\MultiMerchantLogic;
  2071. $maddr = $multiMerchantLogic->getMultiMerchantContact($details['merchant_id']);
  2072. }
  2073. $expressList = Db::name('shop_express')->select();
  2074. // 加载模板
  2075. $eyou = [
  2076. 'maddr' => $maddr,
  2077. 'field' => $details,
  2078. 'ServiceUrl' => url('user/Shop/after_service_update', ['_ajax' => 1]),
  2079. 'StatusArr' => [6, 7, 8],
  2080. 'StatusLog' => $this->shop_model->GetOrderServiceLog($service_id, $this->users, $details['users_id']),
  2081. 'ExpressList' => $expressList,
  2082. ];
  2083. $this->assign('eyou', $eyou);
  2084. return $this->fetch('users/shop_after_service_details');
  2085. }
  2086. // 更新服务单状态
  2087. public function after_service_update()
  2088. {
  2089. if (IS_AJAX_POST) {
  2090. if (empty($this->users_id)) $this->redirect('user/Login/login');
  2091. if ($this->usersTplVersion == 'v3') return $this->after_service_update_v3();
  2092. $post = input('post.');
  2093. if (empty($post['delivery']['cost'])) $post['delivery']['cost'] = 0;
  2094. if (empty($post['delivery']['time'])) $post['delivery']['time'] = date('Y-m-d H:i:s');
  2095. // 查询条件
  2096. $where = [
  2097. 'users_id' => $this->users_id,
  2098. 'service_id' => intval($post['service_id']),
  2099. ];
  2100. // 更新数据
  2101. $update = [
  2102. 'status' => 4,
  2103. 'users_delivery' => serialize($post['delivery']),
  2104. 'update_time' => getTime(),
  2105. ];
  2106. $resultID = $this->shop_order_service_db->where($where)->update($update);
  2107. if (!empty($resultID)) {
  2108. // 添加记录单
  2109. $post['users_id'] = $this->users_id;
  2110. $this->shop_common->AddOrderServiceLog($post, 1);
  2111. $this->success('操作成功');
  2112. } else {
  2113. $this->error('操作失败');
  2114. }
  2115. }
  2116. }
  2117. // 第三套模板 更新服务单状态
  2118. public function after_service_update_v3()
  2119. {
  2120. if (IS_AJAX_POST) {
  2121. $post = input('post.');
  2122. // 查询条件
  2123. $where = [
  2124. 'users_id' => $this->users_id,
  2125. 'service_id' => intval($post['service_id']),
  2126. 'details_id' => intval($post['details_id']),
  2127. ];
  2128. // 更新数据
  2129. $update = [
  2130. 'status' => 4,
  2131. 'users_delivery' => serialize($post['delivery']),
  2132. 'update_time' => getTime(),
  2133. ];
  2134. $resultID = $this->shop_order_service_db->where($where)->update($update);
  2135. if (!empty($resultID)) {
  2136. // 添加记录单
  2137. $post['users_id'] = $this->users_id;
  2138. $this->shop_common->AddOrderServiceLog($post, 1);
  2139. $this->success('操作成功!');
  2140. } else {
  2141. $this->error('操作失败!');
  2142. }
  2143. }
  2144. }
  2145. /*------陈风任---2021-1-12---售后服务(退换货)------结束------*/
  2146. // 移动端订单列表瀑布流分页专业ajax加载接口 20210709大黄
  2147. public function ajax_shop_centre_page()
  2148. {
  2149. // 基础查询条件
  2150. $OrderWhere = [
  2151. 'users_id' => $this->users_id,
  2152. 'lang' => $this->home_lang,
  2153. ];
  2154. // 应用搜索条件
  2155. $keywords = input('param.keywords/s');
  2156. if (!empty($keywords)) $OrderWhere['order_code'] = ['LIKE', "%{$keywords}%"];
  2157. // 订单状态搜索
  2158. $select_status = input('param.select_status');
  2159. $pagesize = input('param.pagesize',10);
  2160. if (!empty($select_status)) {
  2161. if ('daifukuan' === $select_status) $select_status = 0;
  2162. if (3 == $select_status) $OrderWhere['is_comment'] = 0;
  2163. $OrderWhere['order_status'] = $select_status;
  2164. }
  2165. // 分页查询逻辑
  2166. $paginate_type = isMobile() ? 'usersmobile' : 'userseyou';
  2167. $query_get = input('get.');
  2168. $paginate = array(
  2169. 'type' => $paginate_type,
  2170. 'var_page' => config('paginate.var_page'),
  2171. 'query' => $query_get,
  2172. );
  2173. $pages = Db::name('shop_order')
  2174. ->field("*")
  2175. ->where($OrderWhere)
  2176. ->order('add_time desc')
  2177. ->paginate($pagesize, false, $paginate);
  2178. $result['list'] = $pages->items();
  2179. $result['pages'] = $pages;
  2180. // 搜索名称时,查询订单明细表商品名称
  2181. if (empty($result['list']) && !empty($keywords)) {
  2182. $Data = model('Shop')->QueryOrderList($pagesize, $this->users_id, $keywords, $query_get);
  2183. $result['list'] = $Data['list'];
  2184. $result['pages'] = $Data['pages'];
  2185. }
  2186. /*规格值ID预处理*/
  2187. $SpecValueArray = Db::name('product_spec_value')->field('aid,spec_value_id')->select();
  2188. $SpecValueArray = group_same_key($SpecValueArray, 'aid');
  2189. $ReturnData = [];
  2190. foreach ($SpecValueArray as $key => $value) {
  2191. $ReturnData[$key] = [];
  2192. foreach ($value as $kk => $vv) {
  2193. array_push($ReturnData[$key], $vv['spec_value_id']);
  2194. }
  2195. }
  2196. /* END */
  2197. if (!empty($result['list'])) {
  2198. // 订单数据处理
  2199. $controller_name = 'Product';
  2200. // 获取当前链接及参数,用于手机端查询快递时返回页面
  2201. $OrderIds = [];
  2202. $ReturnUrl = request()->url(true);
  2203. foreach ($result['list'] as $key => $value) {
  2204. $DetailsWhere['a.users_id'] = $value['users_id'];
  2205. $DetailsWhere['a.order_id'] = $value['order_id'];
  2206. // 查询订单明细表数据
  2207. $result['list'][$key]['details'] = Db::name('shop_order_details')->alias('a')
  2208. ->field('a.*, b.service_id, c.is_del')
  2209. ->join('__SHOP_ORDER_SERVICE__ b', 'a.details_id = b.details_id', 'LEFT')
  2210. ->join('__ARCHIVES__ c', 'a.product_id = c.aid', 'LEFT')
  2211. ->order('a.product_price desc, a.product_name desc')
  2212. ->where($DetailsWhere)
  2213. ->select();
  2214. $array_new = get_archives_data($result['list'][$key]['details'], 'product_id');
  2215. foreach ($result['list'][$key]['details'] as $kk => $vv) {
  2216. // 产品规格处理
  2217. $spec_data = unserialize($vv['data']);
  2218. if (!in_array($vv['order_id'], $OrderIds) && 0 == $value['order_status']) {
  2219. if (!empty($spec_data['spec_value_id'])) {
  2220. $spec_value_id = $spec_data['spec_value_id'];
  2221. if (!in_array($spec_value_id, $ReturnData[$vv['product_id']])) {
  2222. // 用于更新订单数据
  2223. array_push($OrderIds, $vv['order_id']);
  2224. // 修改循环内的订单状态进行逻辑计算
  2225. $value['order_status'] = 4;
  2226. // 修改主表数据,确保输出数据正确
  2227. $result['list'][$key]['order_status'] = 4;
  2228. // 用于追加订单操作记录
  2229. $OrderIds_[]['order_id'] = $vv['order_id'];
  2230. }
  2231. }
  2232. }
  2233. $product_spec_list = [];
  2234. if (!empty($spec_data['spec_value'])) {
  2235. $spec_value_arr = explode('<br/>', htmlspecialchars_decode($spec_data['spec_value']));
  2236. foreach ($spec_value_arr as $sp_key => $sp_val) {
  2237. $sp_arr = explode(':', $sp_val);
  2238. if (trim($sp_arr[0]) && !empty($sp_arr[0])) {
  2239. $product_spec_list[] = [
  2240. 'name' => !empty($sp_arr[0]) ? trim($sp_arr[0]) : '',
  2241. 'value' => !empty($sp_arr[1]) ? trim($sp_arr[1]) : '',
  2242. ];
  2243. }
  2244. }
  2245. }
  2246. $result['list'][$key]['details'][$kk]['product_spec_list'] = $product_spec_list;
  2247. // 产品内页地址
  2248. if (!empty($array_new[$vv['product_id']]) && 0 == $vv['is_del']) {
  2249. // 商品存在
  2250. $arcurl = urldecode(arcurl('home/'.$controller_name.'/view', $array_new[$vv['product_id']]));
  2251. $has_deleted = 0;
  2252. $msg_deleted = '';
  2253. } else {
  2254. // 商品不存在
  2255. $arcurl = urldecode(url('home/View/index', ['aid'=>$vv['product_id']]));
  2256. $has_deleted = 1;
  2257. $msg_deleted = '[商品已停售]';
  2258. }
  2259. $result['list'][$key]['details'][$kk]['arcurl'] = $arcurl;
  2260. $result['list'][$key]['details'][$kk]['has_deleted'] = $has_deleted;
  2261. $result['list'][$key]['details'][$kk]['msg_deleted'] = $msg_deleted;
  2262. $result['list'][$key]['details'][$kk]['product_price'] = floatval($vv['product_price']);
  2263. // 图片处理
  2264. $result['list'][$key]['details'][$kk]['litpic'] = handle_subdir_pic(get_default_pic($vv['litpic']));
  2265. // 申请退换货
  2266. $result['list'][$key]['details'][$kk]['ApplyService'] = urldecode(url('user/Shop/after_service_apply', ['details_id' => $vv['details_id']]));
  2267. // 查看售后详情单
  2268. $result['list'][$key]['details'][$kk]['ViewAfterSale'] = urldecode(url('user/Shop/after_service_details', ['service_id' => $vv['service_id']]));
  2269. // 商品评价
  2270. $result['list'][$key]['details'][$kk]['CommentProduct'] = urldecode(url('user/ShopComment/product', ['details_id' => $vv['details_id']]));
  2271. }
  2272. if (empty($value['order_status'])) {
  2273. // 付款地址处理,对ID和订单号加密,拼装url路径
  2274. $Paydata = [
  2275. 'order_id' => $value['order_id'],
  2276. 'order_code' => $value['order_code']
  2277. ];
  2278. // 先 json_encode 后 md5 加密信息
  2279. $Paystr = md5(json_encode($Paydata));
  2280. // 清除之前的 cookie
  2281. Cookie::delete($Paystr);
  2282. // 存入 cookie
  2283. cookie($Paystr, $Paydata);
  2284. // 跳转链接
  2285. $result['list'][$key]['PaymentUrl'] = urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$Paystr]));
  2286. }
  2287. // 封装取消订单JS
  2288. $result['list'][$key]['CancelOrder'] = " onclick=\"CancelOrder('{$value['order_id']}');\" ";
  2289. // 获取订单状态
  2290. $order_status_arr = Config::get('global.order_status_arr');
  2291. $result['list'][$key]['order_status_name'] = $order_status_arr[$value['order_status']];
  2292. // 获取订单支付方式名称
  2293. $pay_method_arr = Config::get('global.pay_method_arr');
  2294. if (!empty($value['payment_method']) && !empty($value['pay_name'])) {
  2295. $result['list'][$key]['pay_name'] = !empty($pay_method_arr[$value['pay_name']]) ? $pay_method_arr[$value['pay_name']] : '第三方支付';
  2296. } else {
  2297. if (!empty($value['pay_name'])) {
  2298. $result['list'][$key]['pay_name'] = !empty($pay_method_arr[$value['pay_name']]) ? $pay_method_arr[$value['pay_name']] : '第三方支付';
  2299. } else {
  2300. $result['list'][$key]['pay_name'] = '在线支付';
  2301. }
  2302. }
  2303. // 封装订单查询详情链接
  2304. $result['list'][$key]['OrderDetailsUrl'] = urldecode(url('user/Shop/shop_order_details',['order_id'=>$value['order_id']]));
  2305. // 封装订单催发货JS
  2306. $result['list'][$key]['OrderRemind'] = " onclick=\"OrderRemind('{$value['order_id']}','{$value['order_code']}');\" ";
  2307. // 封装确认收货JS
  2308. $result['list'][$key]['Confirm'] = " onclick=\"Confirm('{$value['order_id']}','{$value['order_code']}');\" ";
  2309. //售后
  2310. $result['list'][$key]['ServiceList'] = urldecode(url('user/Shop/service_list', ['order_id' => $value['order_id']]));
  2311. //评价
  2312. $result['list'][$key]['AddProduct'] = urldecode(url('user/ShopComment/comment_list', ['order_id' => $value['order_id']]));
  2313. // 封装查询物流链接
  2314. $result['list'][$key]['LogisticsInquiry'] = $MobileExpressUrl = '';
  2315. if (('2' == $value['order_status'] || '3' == $value['order_status']) && empty($value['prom_type'])) {
  2316. // 物流查询接口
  2317. if (isMobile()) {
  2318. $ExpressUrl = "https://m.kuaidi100.com/index_all.html?type=".$value['express_code']."&postid=".$value['express_order']."&callbackurl=".$ReturnUrl;
  2319. } else {
  2320. $ExpressUrl = "https://www.kuaidi100.com/chaxun?com=".$value['express_code']."&nu=".$value['express_order'];
  2321. }
  2322. // 微信端、小程序使用跳转方式进行物流查询
  2323. $result['list'][$key]['MobileExpressUrl'] = $ExpressUrl;
  2324. // PC端,手机浏览器使用弹框方式进行物流查询
  2325. $result['list'][$key]['LogisticsInquiry'] = " onclick=\"LogisticsInquiry('{$ExpressUrl}');\" ";
  2326. }
  2327. $result['list'][$key]['order_amount'] = floatval($value['order_amount']);
  2328. $result['list'][$key]['order_total_amount'] = floatval($value['order_total_amount']);
  2329. $result['list'][$key]['shipping_fee'] = is_numeric($value['shipping_fee']) ? floatval($value['shipping_fee']) : $value['shipping_fee'];
  2330. // 默认为空
  2331. $result['list'][$key]['hidden'] = '';
  2332. }
  2333. // 更新产品规格异常的订单,更新为订单过期
  2334. if (!empty($OrderIds)) {
  2335. // 更新订单
  2336. $UpData = [
  2337. 'order_status' => 4,
  2338. 'update_time' => getTime()
  2339. ];
  2340. Db::name('shop_order')->where('order_id', 'IN', $OrderIds)->update($UpData);
  2341. // 追加订单操作记录
  2342. AddOrderAction($OrderIds_, $this->users_id, 0, 4, 0, 0, '订单过期!', '规格更新后部分产品规格不存在,订单过期!');
  2343. }
  2344. // 传入JS参数
  2345. $data['shop_order_cancel'] = url('user/Shop/shop_order_cancel', ['_ajax'=>1], true, false, 1, 1, 0);
  2346. $data['shop_member_confirm'] = url('user/Shop/shop_member_confirm');
  2347. $data['shop_order_remind'] = url('user/Shop/shop_order_remind');
  2348. $data_json = json_encode($data);
  2349. $version = getCmsVersion();
  2350. // 循环中第一个数据带上JS代码加载
  2351. $result['list'][0]['hidden'] = <<<EOF
  2352. <script type="text/javascript">
  2353. var d62a4a8743a94dc0250be0c53f833b = {$data_json};
  2354. </script>
  2355. <script type="text/javascript" src="{$this->root_dir}/public/static/common/js/tag_sporderlist.js?v={$version}"></script>
  2356. EOF;
  2357. $this->success('请求成功!','',$result);
  2358. }else{
  2359. $this->error('暂无数据!');
  2360. }
  2361. }
  2362. }