Replacebuy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\replacebuy\shop\controller;
  11. use addon\replacebuy\model\GoodsSku;
  12. use app\model\member\Member as MemberModel;
  13. use app\shop\controller\BaseShop;
  14. use app\model\goods\GoodsCategory as GoodsCategoryModel;
  15. use app\model\goods\Goods as GoodsModel;
  16. use think\facade\Session;
  17. use app\model\goods\GoodsLabel as GoodsLabelModel;
  18. use app\model\member\MemberAddress as MemberAddressModel;
  19. use app\model\system\Address as AddressModel;
  20. use app\model\express\Config as ExpressConfig;
  21. class Replacebuy extends BaseShop
  22. {
  23. public $buyer_info = [];//买家
  24. public $cart = [];//购物车
  25. public $default_member_info = [];//默认绑定会员
  26. public function __construct()
  27. {
  28. //执行父类构造函数
  29. parent::__construct();
  30. $this->buyer_info = Session::get("replacebuy_buyer_info") ?? [];
  31. $this->default_member_info = Session::get("default_member_info") ?? [];
  32. $this->address_id = Session::get("address_id") ?? 0;
  33. $this->replace = [
  34. 'REPLACEBUY_CSS' => __ROOT__ . '/addon/replacebuy/shop/view/public/css',
  35. 'REPLACEBUY_JS' => __ROOT__ . '/addon/replacebuy/shop/view/public/js',
  36. 'REPLACEBUY_IMG' => __ROOT__ . '/addon/replacebuy/shop/view/public/img',
  37. ];
  38. }
  39. /**
  40. * 代客下单页面
  41. */
  42. public function index()
  43. {
  44. Session::set("address_id", []);
  45. //新增
  46. Session::set("replacebuy_buyer_info", []);
  47. Session::set("r_cart", []);
  48. //修改
  49. // $this->assign("buyer_info", $this->buyer_info);
  50. $this->assign("buyer_info", []);
  51. $cart = Session::get("r_cart") ?? [];
  52. if (!empty($cart)) {
  53. foreach ($cart as $k => $item) {
  54. $goods = new GoodsModel();
  55. $sku_info = $goods->getGoodsSkuInfo([ ['goods_state', '=', 1],['is_delete', '=', 0], ['sku_id', '=', $item['sku_id'] ], ['site_id', '=', $this->site_id] ], 'discount_price');
  56. if (!empty($sku_info['data'])) {
  57. $cart[$k]['sku_price'] = $sku_info['data']['discount_price'];
  58. } else {
  59. unset($cart[$k]);
  60. }
  61. }
  62. Session::set("r_cart", $cart);
  63. }
  64. $this->assign("cart", $cart);
  65. //获取商品分类
  66. $goods_catrgory_model = new GoodsCategoryModel();
  67. $goods_catrgory_list = $goods_catrgory_model->getCategoryList([ [ 'site_id', "=", $this->site_id ], [ "pid", "=", 0 ], [ "is_show", "=", 0 ] ]);
  68. $this->assign("goods_catrgory_list", $goods_catrgory_list[ "data" ]);
  69. //获取商品分组
  70. $goods_label_model = new GoodsLabelModel();
  71. $label_list = $goods_label_model->getLabelList([ [ 'site_id', '=', $this->site_id ] ], 'id,label_name', 'create_time desc');
  72. $label_list = $label_list[ 'data' ];
  73. $this->assign("label_list", $label_list);
  74. return $this->fetch("replacebuy/index" , [] , $this->replace);
  75. }
  76. /**
  77. * 提交订单页面
  78. */
  79. public function order()
  80. {
  81. $this->assign("buyer_info", $this->buyer_info);
  82. $cart = Session::get("r_cart") ?? [];
  83. if (!empty($cart)) {
  84. foreach ($cart as $k => $item) {
  85. $goods = new GoodsModel();
  86. $sku_info = $goods->getGoodsSkuInfo([ ['goods_state', '=', 1],['is_delete', '=', 0], ['sku_id', '=', $item['sku_id'] ], ['site_id', '=', $this->site_id] ], 'discount_price');
  87. if (!empty($sku_info['data'])) {
  88. $cart[$k]['sku_price'] = $sku_info['data']['discount_price'];
  89. } else {
  90. unset($cart[$k]);
  91. }
  92. }
  93. Session::set("r_cart", $cart);
  94. }
  95. $this->assign("cart", $cart);
  96. //渲染缓存中的地址信息
  97. // $this->assign("address_id", $this->address_id);
  98. //查询省级数据列表
  99. $address_model = new AddressModel();
  100. $list = $address_model->getAreaList([["pid", "=", 0], ["level", "=", 1]]);
  101. $this->assign("province_list", $list["data"]);
  102. //查询是否开启快递配送
  103. $express_config_model = new ExpressConfig();
  104. $express_config_result = $express_config_model->getExpressConfig($this->site_id);
  105. $express_config = $express_config_result[ "data" ];
  106. $shop_goods[ "express_config" ] = $express_config;
  107. $is_use = 0;
  108. if ($shop_goods[ "express_config" ][ "is_use" ] == 1) {
  109. $is_use = 1;
  110. }
  111. $this->assign("is_use", $is_use);
  112. //查询默认地址
  113. if ($this->address_id){
  114. $this->assign("address_id", $this->address_id);
  115. }else{
  116. $member_address = new MemberAddressModel();
  117. $info = $member_address->getMemberDefault($this->buyer_info['member_id']);
  118. $address_id = 0;
  119. if ($info){
  120. $address_id = $info['id'];
  121. }
  122. Session::set("address_id", $address_id);
  123. $this->assign("address_id", $address_id);
  124. }
  125. return $this->fetch("replacebuy/order", [] , $this->replace);
  126. }
  127. /**
  128. * 获取商品列表
  129. */
  130. public function getGoodsSkuList()
  131. {
  132. if (request()->isAjax()) {
  133. $page_index = input('page', 1);
  134. $page_size = input('page_size', PAGE_LIST_ROWS);
  135. $search_text = input('search_text', "");
  136. $category_id = input('category_id', '');
  137. $label_id = input('label_id', "");
  138. $condition = [
  139. [ 'g.goods_state', '=', 1 ],
  140. [ 'g.is_delete', '=', 0 ],
  141. [ 'g.site_id', '=', $this->site_id ],
  142. [ 'g.is_virtual', "=", 0 ]
  143. ];
  144. if (!empty($search_text)) {
  145. $condition[] = [ 'gs.sku_name|gs.sku_no', 'like', '%' . $search_text . '%' ];
  146. }
  147. if (!empty($category_id)) {
  148. $condition[] = [ 'g.category_id', 'like', '%,' . $category_id . ',%' ];
  149. }
  150. if (!empty($label_id)) {
  151. $condition[] = [ 'g.label_id', '=', $label_id ];
  152. }
  153. $goods_sku_model = new GoodsSku();
  154. $res = $goods_sku_model->getGoodsSkuPageList($condition, $page_index, $page_size);
  155. return $res;
  156. }
  157. }
  158. /**
  159. * 选择会员
  160. */
  161. public function loginBuyer()
  162. {
  163. if (request()->isAjax()) {
  164. $member_search = input("member_search", '');
  165. $member_id = input('member_id','');
  166. $condition = [];
  167. $condition[] = [ 'mobile|email|username', '=', $member_search ];
  168. $condition[] = [ 'site_id', '=', $this->site_id ];
  169. $condition[] = [ 'member_id', '=', $member_id ];
  170. $member_model = new MemberModel();
  171. $member_info_result = $member_model->getMemberInfo($condition);
  172. $member_info = $member_info_result[ "data" ];
  173. if (empty($member_info))
  174. return $member_model->error([], "账号不存在!");
  175. Session::set("replacebuy_buyer_info", $member_info);
  176. Session::set("address_id", []);
  177. return $member_info_result;
  178. }
  179. }
  180. /**
  181. * 会员列表
  182. */
  183. public function getMemberList()
  184. {
  185. $page_index = input('page', 1);
  186. $page_size = input('page_size', PAGE_LIST_ROWS);
  187. $member_search = input("member_search", '');
  188. $condition = [];
  189. $condition[] = [ 'mobile|email|username|nickname', 'like', '%'.$member_search.'%' ];
  190. $condition[] = [ 'site_id', '=', $this->site_id ];
  191. $member_model = new MemberModel();
  192. $list = $member_model->getMemberPageList($condition,$page_index,$page_size,'','member_id,headimg,nickname,username,mobile,point,balance,balance_money');
  193. return $list;
  194. }
  195. /**
  196. * 注销会员
  197. */
  198. public function logoutBuyer()
  199. {
  200. if (request()->isAjax()) {
  201. Session::set("replacebuy_buyer_info", []);
  202. $member_model = new MemberModel();
  203. return $member_model->success();
  204. }
  205. }
  206. /**
  207. * 购物车同步
  208. */
  209. public function cart()
  210. {
  211. $cart_json = input("cart_json", "");
  212. $temp_array = [];
  213. if (!empty($cart_json)) {
  214. $temp_array = json_decode($cart_json, true);
  215. }
  216. Session::set("r_cart", $temp_array);
  217. }
  218. /**
  219. * 得到买家id
  220. */
  221. public function buyer()
  222. {
  223. //查看是否登陆了会员
  224. if (!empty($this->buyer_info)) {
  225. $buyer_member_id = $this->buyer_info[ "member_id" ];
  226. } else {
  227. $buyer_member_id = $this->default_member_info[ "member_id" ];
  228. }
  229. return $buyer_member_id;
  230. }
  231. /**
  232. * 新增用户收货地址
  233. */
  234. public function addAddress()
  235. {
  236. if (request()->isAjax()) {
  237. $data = [
  238. 'site_id' => $this->site_id,
  239. 'member_id' => $this->buyer(),
  240. 'name' => input("name", ''),
  241. 'mobile' => input("mobile", ''),
  242. 'telephone' => input("telephone", ''),
  243. 'province_id' => input("province_id", ''),
  244. 'city_id' => input("city_id", ''),
  245. 'district_id' => input("district_id", ''),
  246. 'community_id' => input("community_id", ''),
  247. 'address' => input("address", ''),
  248. 'full_address' => input("full_address", ''),
  249. 'longitude' => input("longitude", ''),
  250. 'latitude' => input("latitude", ''),
  251. 'is_default' => 1
  252. ];
  253. $member_address = new MemberAddressModel();
  254. $res = $member_address->addMemberAddress($data);
  255. return $res;
  256. }
  257. }
  258. /**
  259. * 用户地址分页列表信息
  260. */
  261. public function addressPage()
  262. {
  263. if (request()->isAjax()) {
  264. $page = input('page', 1);
  265. $page_size = input('page_size', PAGE_LIST_ROWS);
  266. $type = input('type', 1);
  267. $member_address = new MemberAddressModel();
  268. $condition = [
  269. [ 'member_id', '=', $this->buyer() ],
  270. [ 'site_id', '=', $this->site_id ]
  271. ];
  272. if(!empty($type)){
  273. $condition[] = ['type', '=', $type];
  274. }
  275. // $list = $member_address->getMemberAddressPageList($condition, $page, $page_size);
  276. $list = $member_address->getMemberAddressList($condition);
  277. return $list;
  278. }
  279. }
  280. /**
  281. * 选择地址
  282. */
  283. public function choiceAddress()
  284. {
  285. if (request()->isAjax()) {
  286. $address_id = input('address_id', 0);
  287. Session::set("address_id", $address_id);
  288. }
  289. }
  290. }