Index.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Banner;
  4. use app\api\model\Category;
  5. use app\api\model\CoinRecord;
  6. use app\api\model\Detail;
  7. use app\api\model\Goods;
  8. use app\api\model\MoneyRecord;
  9. use app\api\model\Order;
  10. use app\api\model\PriceRange;
  11. use app\api\model\Prizerecord;
  12. use app\api\model\RechargeList;
  13. use app\api\model\RechargeOrder;
  14. use app\api\model\SearchHistory;
  15. use app\api\model\Setting;
  16. use app\api\model\Box;
  17. use app\api\model\Star;
  18. use app\api\model\Text;
  19. use app\common\controller\Api;
  20. use think\Db;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\Exception;
  24. use think\exception\DbException;
  25. use app\api\library\Retail;
  26. use think\Validate;
  27. /**
  28. * 首页接口
  29. */
  30. class Index extends Api
  31. {
  32. protected $noNeedLogin = ['baseInfo', 'newestOpen', 'hotBox', 'cheapBox', 'recommend', 'categoryList',
  33. 'boxList', 'boxListByCategory', 'boxDetail', 'boxOpenRecord', 'tryBoxDetail', 'priceRange', 'rechargeList', 'getServiceInfo',
  34. 'getWechatLoginUrl', "getVirData", "sumUv", "getServiceurl", "lunbobox", 'boximages'];
  35. protected $noNeedRight = ['*'];
  36. protected $lockUserId = ['coinPay'];
  37. public function sumUv()
  38. {
  39. $ip = request()->ip();
  40. $check = db("uv")->where(['ip' => $ip])->whereTime("createtime", "today")->find();
  41. if (empty($check)) {
  42. db("uv")->insert(['ip' => $ip, "createtime" => time()]);
  43. }
  44. $this->success("记录成功");
  45. }
  46. /**
  47. *获取开盒假数据
  48. */
  49. public function getVirData()
  50. {
  51. $page = request()->param("page") ?? 1;
  52. $limit = request()->param("limit") ?? 10;
  53. $domain = request()->domain();
  54. $list = db("virtuial")->alias("a")->join("goods b", "b.id = a.goods_id")->order("a.add_time desc")->field("a.*,FROM_UNIXTIME(a.add_time,'%Y-%m-%d %H:%i') createtime,b.goods_name, b.image,b.coin_price price")->page($page, $limit)->select();
  55. foreach ($list as $k => $v) {
  56. $list[$k]['avatar'] = $domain . $v['avatar'];
  57. //$list[$k]['image'] = $domain.$v['image'];
  58. $list[$k]['image'] = $v['image'];
  59. }
  60. $total = db("virtuial")->order("add_time desc")->count();
  61. // print_r($list);die;
  62. $this->success("获取成功", ["list" => $list, "total" => $total]);
  63. }
  64. public function lunbobox()
  65. {
  66. $status = 1;
  67. $pagesize = input('pagesize/d', 10);
  68. $page = input('page/d', 1);
  69. $statusList = [1 => 'bag', 2 => 'exchange'];
  70. if (!isset($statusList[$status])) {
  71. $this->error('状态有误');
  72. }
  73. $status = $statusList[$status];
  74. $order = 'prize.id desc';
  75. if ('exchange' == $status) {
  76. $order = 'exchange_time desc';
  77. }
  78. $list = Prizerecord::alias('prize')
  79. ->distinct(true)
  80. ->field('prize.goods_name,prize.goods_image')
  81. ->field('prize.box_id')
  82. ->field('user.nickname,user.avatar')
  83. ->join('user user', 'user.id = prize.user_id')
  84. ->order($order)
  85. ->limit(20)
  86. ->select();
  87. $this->success('查询成功', $list);
  88. }
  89. /**
  90. * 基础信息
  91. * @author fuyelk <fuyelk@fuyelk.com>
  92. */
  93. public function baseInfo()
  94. {
  95. // 查询设置
  96. $setting = Setting::get(1);
  97. // 查询轮播图
  98. $banner = Banner::where('place', 'index')->order('weigh', 'desc')->select();
  99. $bannerlist = [];
  100. foreach ($banner as $item) {
  101. $bannerlist[] = [
  102. 'type' => $item->type,
  103. 'image' => cdnurl($item->image, true),
  104. 'value' => $item->value
  105. ];
  106. }
  107. $ret = [
  108. 'logo' => cdnurl($setting->logo_image, true), // 首页LOGO
  109. 'banner' => $bannerlist
  110. ];
  111. $this->success('查询成功', $ret);
  112. }
  113. public function sList()
  114. {
  115. $status = input('status/d');
  116. $pagesize = input('pagesize/d', 10);
  117. $page = input('page/d', 1);
  118. $statusList = [1 => 'bag', 2 => 'exchange'];
  119. if (!isset($statusList[$status])) {
  120. $this->error('状态有误');
  121. }
  122. $status = $statusList[$status];
  123. $order = 'prize.id desc';
  124. if ('exchange' == $status) {
  125. $order = 'exchange_time desc';
  126. }
  127. $order = Db::table('box_shai')->where('switch', '1')->order('create_time', 'desc')->limit(($page - 1) * $pagesize, $pagesize)->select();
  128. $list2 = array(
  129. 'id' => 1,
  130. 'stx' => 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic3.zhimg.com%2F50%2Fv2-6f1c492cbdfe3c24aae44e935a796d5a_hd.jpg&refer=http%3A%2F%2Fpic3.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1632275904&t=20baefb74a9f7b152b601ec58726f1e7',
  131. 'smc' => '这个是名称',
  132. 'ssj' => '前天',
  133. 'szw' => '这个盲盒很好玩,我很喜欢,我也介绍了很多人过来完',
  134. 'simg' => array(
  135. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  136. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  137. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  138. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  139. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  140. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  141. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  142. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  143. 'https://img.alicdn.com/imgextra/i4/0/O1CN01CHW9ak1RdPu9Vg1r6_!!0-rate.jpg_400x400.jpg',
  144. )
  145. );
  146. /* ->each(function ($item) use ($status) {
  147. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  148. $item->box_coin_price = intval($item->box_coin_price);
  149. $item->box_rmb_price = floatval($item->box_rmb_price);
  150. if ('exchange' == $status) {
  151. $item->time = date('Y-m-d H:i:s', $item->exchange_time);
  152. } else {
  153. $item->time = date('Y-m-d H:i:s', $item->create_time);
  154. }
  155. $item->hidden(['create_time', 'exchange_time']);
  156. });
  157. */
  158. //print_r($list);
  159. //exit;
  160. $ym = 'https://' . $_SERVER['HTTP_HOST'];
  161. foreach ($order as $k => $v) {
  162. // $order[$k]['box_tx']=$ym.$v['box_tx'];
  163. $order[$k]['box_img'] = explode(',', $v['box_img']);
  164. // foreach ($order[$k]['box_img'] as $k2=>$v2){
  165. // $order[$k]['box_img'][$k2]=$ym.$v2;
  166. // }
  167. if (time() - $v['create_time'] < 60 * 60) {
  168. $t = round((time() - $v['create_time']) / 60);
  169. if ($t == 0) {
  170. $t = 1;
  171. }
  172. $order[$k]['box_sj'] = $t . '分钟前';
  173. } else if (time() - $v['create_time'] > 60 * 60 && time() - $v['create_time'] < 60 * 60 * 24) {
  174. $t = round((time() - $v['create_time']) / 60 / 60);
  175. $order[$k]['box_sj'] = $t . '小时前';
  176. } else if (time() - $v['create_time'] > 60 * 60 * 24 && time() - $v['create_time'] < 60 * 60 * 24 * 7) {
  177. $t = round((time() - $v['create_time']) / 60 / 60 / 24);
  178. $order[$k]['box_sj'] = $t . '天前';
  179. } else if (time() - $v['create_time'] > 60 * 60 * 24 * 7) {
  180. $t = round((time() - $v['create_time']) / 60 / 60 / 24 / 7);
  181. $order[$k]['box_sj'] = $t . '周前';
  182. }
  183. }
  184. //print_r($order);
  185. $list = array(
  186. 'data' => $order
  187. );
  188. //$list=$order;
  189. foreach ($list as $k => $v) {
  190. // print_r($v);
  191. }
  192. $this->success('查询成功', $list);
  193. }
  194. /**
  195. * 最新开箱的盲盒列表
  196. * @author fuyelk <fuyelk@fuyelk.com>
  197. */
  198. public function newestOpen()
  199. {
  200. // 查询数量
  201. $limit = 5;
  202. // 查询用户最新购买的100个盲盒ID
  203. $boxIds = Order::field('box_id')->order('id', 'desc')->limit(100)->column('box_id');
  204. //Array ( [0] => 36 [1] => 37 [2] => 38 [3] => 39 [4] => 40 [5] => 36 [6] => 36 [7] => 36 [8] => 36 [9] => 41 [10] => 39 [11] => 39 [12] => 36 [13] => 37 [14] => 41 [15] => 41 )
  205. $boxIds = array_unique($boxIds);
  206. $boxAdd = [];
  207. // 新开箱不足5个,则按盲盒上架顺序补齐
  208. if (count($boxIds) < $limit) {
  209. $boxAdd = Box::whereNotIn('id', $boxIds)->order('update_time', 'desc')->limit($limit - count($boxIds))->column('id');
  210. }
  211. //Array ( [0] => 36 [1] => 37 [2] => 38 [3] => 39 [4] => 40 [5] => 41 )
  212. $boxIds = array_merge($boxIds, $boxAdd);
  213. // 查询空盲盒
  214. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  215. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  216. $list = Box::alias('box')
  217. ->field('id box_id,box_name,coin_price,box_banner_images')
  218. ->whereNotIn('id', $emptyBoxIds)
  219. ->whereIn('id', $boxIds)
  220. ->order('id', 'desc')
  221. ->select();
  222. //print_r($list);die;
  223. foreach ($list as $item) {
  224. // 查询前5个商品图片
  225. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(4)->column('goods_id');
  226. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  227. foreach ($goods_images as &$image) {
  228. $image = cdnurl($image, true);
  229. }
  230. // 查询最低价
  231. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  232. $item->goods_num = count($allGoods);
  233. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  234. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  235. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  236. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  237. $item->goods_images = $goods_images;
  238. }
  239. //print_r($list);die;
  240. $this->success('查询成功', $list);
  241. }
  242. public function boximages()
  243. {
  244. $box_id = input('box_id');
  245. $tagName = [
  246. 'normal' => '普通',
  247. 'rare' => '稀有',
  248. 'supreme' => '史诗',
  249. 'legend' => '传说',
  250. ];
  251. // $firstGoods = db('box_detail')->where('box_id', 49)->find();
  252. // 查询商品id及概率
  253. $detail = db('box_detail')->where('box_id', $box_id)->order('weigh', 'desc')->column('goods_id');
  254. // 查询全部商品
  255. $moreGoods = db('goods')->field('image')
  256. ->where('status', 'online')
  257. ->whereIn('id', $detail)
  258. ->select();
  259. $ret = [
  260. 'goodsimagelist' => $moreGoods,
  261. ];
  262. $this->success('查询成功', $ret);
  263. }
  264. /**
  265. * 热门盲盒
  266. * @author fuyelk <fuyelk@fuyelk.com>
  267. */
  268. public function hotBox()
  269. {
  270. $pagesize = input('pagesize/d', 10);
  271. $page = input('page/d', 1);
  272. // 查询空盲盒
  273. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  274. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  275. $list = Box::alias('box')
  276. ->field('id box_id,box_name,coin_price')
  277. ->where('is_hot', 1)
  278. ->whereNotIn('box.id', $emptyBoxIds)
  279. ->order('box.id', 'desc')
  280. ->paginate($pagesize, false, ['page' => $page])
  281. ->each(function ($item) {
  282. // 查询前6个商品图片
  283. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  284. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  285. foreach ($goods_images as &$image) {
  286. $image = cdnurl($image, true);
  287. }
  288. // 查询最低价
  289. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  290. $item->goods_num = count($allGoods);
  291. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  292. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  293. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  294. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  295. $item->goods_images = $goods_images;
  296. });
  297. $ret = [
  298. 'banner' => cdnurl(Setting::getSetting('hot_box_banner'), true),
  299. 'list' => $list
  300. ];
  301. $this->success('查询成功', $ret);
  302. }
  303. /**
  304. * 低价盲盒
  305. * @author fuyelk <fuyelk@fuyelk.com>
  306. */
  307. public function cheapBox()
  308. {
  309. $pagesize = input('pagesize/d', 10);
  310. $page = input('page/d', 1);
  311. // 查询空盲盒
  312. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  313. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  314. $list = Box::alias('box')
  315. ->field('id box_id,box_name,coin_price')
  316. ->where('is_cheap', 1)
  317. ->whereNotIn('box.id', $emptyBoxIds)
  318. ->order('box.id', 'desc')
  319. ->paginate($pagesize, false, ['page' => $page])
  320. ->each(function ($item) {
  321. // 查询前6个商品图片
  322. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  323. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  324. foreach ($goods_images as &$image) {
  325. $image = cdnurl($image, true);
  326. }
  327. // 查询最低价
  328. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  329. $item->goods_num = count($allGoods);
  330. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  331. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  332. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  333. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  334. $item->goods_images = $goods_images;
  335. });
  336. $ret = [
  337. 'banner' => cdnurl(Setting::getSetting('cheap_box_banner'), true),
  338. 'list' => $list
  339. ];
  340. $this->success('查询成功', $ret);
  341. }
  342. /**
  343. * 推荐盲盒
  344. * @throws \think\Exception
  345. * @throws \think\db\exception\DataNotFoundException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. * @throws \think\exception\DbException
  348. * @author fuyelk <fuyelk@fuyelk.com>
  349. */
  350. public function recommend()
  351. {
  352. $pagesize = input('pagesize/d', 10);
  353. $page = input('page/d', 1);
  354. // 查询空盲盒
  355. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  356. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  357. $list = Box::alias('box')
  358. ->field('id box_id,box_name,coin_price,box_banner_images,box_foot_images')
  359. ->whereNotIn('id', $emptyBoxIds)
  360. ->order('sort', 'asc')
  361. ->where("switch", 1)
  362. ->paginate($pagesize, false, ['page' => $page])
  363. ->each(function ($item) {
  364. // 查询前6个商品图片
  365. // $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  366. // $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  367. $goods_images = Detail::alias('a')->join("goods b", "b.id = a.goods_id")->where('a.box_id', $item->box_id)->order('a.weigh', 'desc')->limit(6)->column('b.image');
  368. foreach ($goods_images as &$image) {
  369. $image = cdnurl($image, true);
  370. }
  371. // 查询最低价
  372. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  373. $item->goods_num = count($allGoods);
  374. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  375. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  376. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  377. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  378. $item->goods_images = $goods_images;
  379. $box_banner_images = explode(',', $item->box_banner_images);
  380. foreach ($box_banner_images as &$v) {
  381. $v = cdnurl($v, true);
  382. }
  383. $item->box_banner_images = $box_banner_images;
  384. $box_foot_images = explode(',', $item->box_foot_images);
  385. foreach ($box_foot_images as &$v) {
  386. $v = cdnurl($v, true);
  387. }
  388. $item->box_foot_images = $box_foot_images;
  389. });
  390. $this->success('查询成功', $list);
  391. }
  392. /**
  393. * 分类列表
  394. * @author fuyelk <fuyelk@fuyelk.com>
  395. */
  396. public function categoryList()
  397. {
  398. $list = Category::field('id category_id,name')->order('weigh', 'desc')->select();
  399. $this->success('查询成功', $list);
  400. }
  401. /**
  402. * 盲盒列表
  403. * @throws \think\exception\DbException
  404. * @author fuyelk <fuyelk@fuyelk.com>
  405. */
  406. public function boxListByCategory()
  407. {
  408. $pagesize = input('pagesize/d', 10);
  409. $page = input('page/d', 1);
  410. $category_id = input('category_id/d');
  411. $search = input('search', '');
  412. $sort = input('sort', '');
  413. $price = input('price', '');
  414. // 构建筛选条件
  415. $wherePrice = [];
  416. if ($price) {
  417. list($price_low, $price_heigh) = array_slice(explode('-', $price), 0, 2);
  418. if (!is_numeric($price_low) || !is_numeric($price_heigh)) {
  419. $this->error('价格筛选有误');
  420. }
  421. $wherePrice = ['coin_price' => ['between', [$price_low, $price_heigh]]];
  422. }
  423. // 构建排序条件
  424. $order = "";
  425. switch ($sort) {
  426. case '':
  427. $order = "id desc";
  428. break;
  429. case 'new':
  430. $order = "id desc";
  431. break;
  432. case 'combine_desc':
  433. $order = "sales desc";
  434. break;
  435. case 'combine_asc':
  436. $order = "sales asc";
  437. break;
  438. case 'price_desc':
  439. $order = "coin_price desc";
  440. break;
  441. case 'price_asc':
  442. $order = "coin_price asc";
  443. break;
  444. default:
  445. $this->error('排序方式有误');
  446. break;
  447. }
  448. if (empty($category_id) && empty($search)) {
  449. $this->error('查询条件有误');
  450. }
  451. $whereCategory = [];
  452. if (!empty($category_id)) {
  453. $whereCategory = ['category_id' => $category_id];
  454. }
  455. $whereSearch = [];
  456. if (!empty($search)) {
  457. // 过滤非法字符
  458. $search = preg_replace("/[!@#$%^&*=\-+_\[\]{};':\"\/<>?]+/", '', $search);
  459. // 创建搜索记录
  460. if ($this->auth->isLogin()) {
  461. if (!SearchHistory::where(['user_id' => $this->auth->id, 'search' => $search])->value('id')) {
  462. SearchHistory::create(['user_id' => $this->auth->id, 'search' => $search]);
  463. }
  464. }
  465. $whereSearch = "box_name like '%{$search}%'";
  466. }
  467. // 查询空盲盒
  468. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  469. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  470. $list = Box::field('id box_id,box_name,coin_price,box_banner_images')
  471. ->order($order)
  472. ->whereNotIn('id', $emptyBoxIds)
  473. ->where($wherePrice)
  474. ->where($whereCategory)
  475. ->where($whereSearch)
  476. ->paginate($pagesize, false, ['page' => $page])
  477. ->each(function ($item) {
  478. // 查询第一张图片
  479. $goodsid = Detail::order('weigh', 'desc')->where('box_id', $item->box_id)->value('goods_id');
  480. $item->image = '';
  481. if ($goodsid) {
  482. $image = Goods::where('id', $goodsid)->value('image');
  483. $item->image = $image ? cdnurl($image, true) : '';
  484. }
  485. });
  486. $this->success('查询成功', $list);
  487. }
  488. /**
  489. * 搜索
  490. * @throws DbException
  491. * @author fuyelk <fuyelk@fuyelk.com>
  492. */
  493. public function search()
  494. {
  495. $pagesize = input('pagesize/d', 10);
  496. $page = input('page/d', 1);
  497. $category_id = input('category_id/d');
  498. $search = input('search', '');
  499. $sort = input('sort', '');
  500. $price = input('price', '');
  501. // 构建筛选条件
  502. $wherePrice = [];
  503. if ($price) {
  504. list($price_low, $price_heigh) = array_slice(explode('-', $price), 0, 2);
  505. if (!is_numeric($price_low) || !is_numeric($price_heigh)) {
  506. $this->error('价格筛选有误');
  507. }
  508. $wherePrice = ['coin_price' => ['between', [$price_low, $price_heigh]]];
  509. }
  510. // 构建排序条件
  511. $order = "";
  512. switch ($sort) {
  513. case '':
  514. $order = "id desc";
  515. break;
  516. case 'new':
  517. $order = "id desc";
  518. break;
  519. case 'combine_desc':
  520. $order = "sales desc";
  521. break;
  522. case 'combine_asc':
  523. $order = "sales asc";
  524. break;
  525. case 'price_desc':
  526. $order = "coin_price desc";
  527. break;
  528. case 'price_asc':
  529. $order = "coin_price asc";
  530. break;
  531. default:
  532. $this->error('排序方式有误');
  533. break;
  534. }
  535. $whereCategory = [];
  536. if (!empty($category_id)) {
  537. $whereCategory = ['category_id' => $category_id];
  538. }
  539. $whereSearch = [];
  540. if (!empty($search)) {
  541. // 过滤非法字符
  542. $search = preg_replace("/[!@#$%^&*=\-+_\[\]{};':\"\/<>?]+/", '', trim($search));
  543. if (!empty($search)) {
  544. // 创建搜索记录
  545. if ($this->auth->isLogin()) {
  546. if (!SearchHistory::where(['user_id' => $this->auth->id, 'search' => $search])->value('id')) {
  547. SearchHistory::create(['user_id' => $this->auth->id, 'search' => $search]);
  548. }
  549. }
  550. // 构建商品ID搜索
  551. $condition_goods_ids = Goods::field('id')->where('status', 'online')->where("goods_name like '%{$search}%'")->buildSql();
  552. // 构建商品、盲盒绑定
  553. $condition_box_detail = Detail::field('box_id')->where("goods_id in {$condition_goods_ids}")->buildSql();
  554. $whereSearch = "box_name like '%{$search}%' or id in $condition_box_detail";
  555. }
  556. }
  557. // 查询空盲盒
  558. $existboxid = Detail::field('box_id')->distinct(true)->buildSql();
  559. $emptyBoxIds = Box::where('id', 'exp', 'not in ' . $existboxid)->column('id');
  560. $list = Box::alias('box')
  561. ->field('id box_id,box_name,coin_price,box_banner_images')
  562. ->whereNotIn('id', $emptyBoxIds)
  563. ->where($wherePrice)
  564. ->where($whereCategory)
  565. ->where($whereSearch)
  566. ->where(['switch' => 1])
  567. ->order($order)
  568. ->paginate($pagesize, false, ['page' => $page])
  569. ->each(function ($item) {
  570. // 查询前6个商品图片
  571. $firstGoods = Detail::where('box_id', $item->box_id)->order('weigh', 'desc')->limit(6)->column('goods_id');
  572. $goods_images = Goods::whereIn('id', $firstGoods)->column('image');
  573. foreach ($goods_images as &$image) {
  574. $image = cdnurl($image, true);
  575. }
  576. // 查询最低价
  577. $allGoods = Detail::where('box_id', $item->box_id)->column('goods_id');
  578. $item->goods_num = count($allGoods);
  579. $min_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'asc')->value('coin_price');
  580. $item->price_min = round(Setting::getRmbFromCoin($min_coin_price ?: 0), 2);
  581. $max_coin_price = Goods::whereIn('id', $allGoods)->order('coin_price', 'desc')->value('coin_price');
  582. $item->price_max = round(Setting::getRmbFromCoin($max_coin_price ?: 0), 2);
  583. $item->goods_images = $goods_images;
  584. });
  585. $this->success('查询成功', $list);
  586. }
  587. /**
  588. * 盲盒详情
  589. * @throws \think\db\exception\DataNotFoundException
  590. * @throws \think\db\exception\ModelNotFoundException
  591. * @throws \think\exception\DbException
  592. * @author fuyelk <fuyelk@fuyelk.com>
  593. */
  594. public function boxDetail()
  595. {
  596. $box_id = input('box_id/d');
  597. if (empty($box_id)) {
  598. $this->error('请选择盲盒');
  599. }
  600. // 查询金币余额
  601. $mycoin = $this->auth->isLogin() ? $this->auth->coin : 0;
  602. // 查询是否收藏
  603. $is_star = $this->auth->isLogin() ? Star::check($this->auth->id, $box_id) : 0;
  604. // 查询盲盒基础信息
  605. $box = Box::field('box_banner_images,box_banner_images_desc,box_name,coin_price,box_banner_images')->where('id', $box_id)->find();
  606. if (empty($box)) {
  607. $this->error('盲盒有误');
  608. }
  609. $tagName = [
  610. 'normal' => '普通',
  611. 'rare' => '稀有',
  612. 'supreme' => '史诗',
  613. 'legend' => '传说',
  614. ];
  615. // 查询商品id及概率
  616. $detail = Detail::where('box_id', $box_id)->order('weigh desc')->column('rate', 'goods_id');
  617. // // 查询前6个商品
  618. // $firstGoods = Goods::field('image,coin_price,goods_name,tag')
  619. // ->where('status', 'online')
  620. // ->whereIn('id', array_slice(array_keys($detail), 0, 1000))
  621. // ->select();
  622. $firstGoods = Detail::alias("a")->join("goods b", "b.id = a.goods_id")->where('a.box_id', $box_id)->field("b.image,b.xiangqing,b.coin_price,b.goods_name,b.tag")->order("a.weigh desc")->select();
  623. foreach ($firstGoods as &$first) {
  624. $first->image = $first->image ? cdnurl($first->image, true) : $first->image;
  625. $first->tag = $tagName[$first->tag];
  626. // $first->price = round(Setting::getRmbFromCoin($first->coin_price ?: 0), 2);
  627. $first->price = $first->coin_price;
  628. if (!empty($first->xiangqing)) {
  629. if (strstr($first['xiangqing'], ',')) {
  630. $first['xiangqing'] = explode(',', $first['xiangqing']);
  631. // var_dump($first);
  632. // exit();
  633. }
  634. }
  635. $first->hidden(['coin_price']);
  636. }
  637. // var_dump($first);exit();
  638. // 查询全部商品
  639. $moreGoods = Goods::field('id,image,xiangqing,coin_price,goods_name,tag')
  640. ->where('status', 'online')
  641. ->whereIn('id', array_keys($detail))
  642. ->select();
  643. // 整理商品信息并记录每个类别的概率总合
  644. $rateList = [];
  645. foreach ($moreGoods as &$more) {
  646. if (isset($rateList[$more->tag])) {
  647. $rateList[$more->tag] += $detail[$more->id];
  648. } else {
  649. $rateList[$more->tag] = $detail[$more->id];
  650. }
  651. $more->image = $more->image ? cdnurl($more->image, true) : $more->image;
  652. $more->tag = $tagName[$more->tag];
  653. $more->price = round(Setting::getRmbFromCoin($more->coin_price ?: 0), 2);
  654. if (strstr($more->xiangqing, ',')) {
  655. $more->xiangqing = explode(',', $more->xiangqing);
  656. }
  657. $more->hidden(['id,coin_price']);
  658. }
  659. $tags = [
  660. 'normal' => 0,
  661. 'rare' => 0,
  662. 'supreme' => 0,
  663. 'legend' => 0
  664. ];
  665. // 没有的商品概率设为0
  666. foreach ($tags as $tag => &$rate) {
  667. if (isset($rateList[$tag])) {
  668. $rate = $rateList[$tag];
  669. }
  670. }
  671. // 计算全部类别概率总和
  672. $rate_sum = array_sum(array_values($tags));
  673. // 计算每个类别概率
  674. foreach ($tags as $tag => &$rate) {
  675. $rate = $rate_sum ? (round($rate / $rate_sum, 4) * 100) : 0 . '%';
  676. }
  677. // 查询该盲盒开箱记录
  678. $prize = Prizerecord::alias('prize')
  679. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  680. ->field('user.nickname,user.avatar')
  681. ->join('user user', 'user.id = prize.user_id')
  682. ->where('prize.box_id', $box_id)
  683. ->order('prize.id', 'desc')
  684. ->limit(10)
  685. ->select();
  686. foreach ($prize as $prize_item) {
  687. $prize_item->create_time = date('Y-m-d H:i:s', $prize_item->create_time);
  688. $prize_item->avatar = $prize_item->avatar ? cdnurl($prize_item->avatar, true) : letter_avatar($prize_item->nickname);
  689. $prize_item->goods_image = $prize_item->goods_image ? cdnurl($prize_item->goods_image, true) : '';
  690. }
  691. $box_banner_images = [];
  692. $box_banner = [];
  693. $box->box_banner_images = explode(',', $box->box_banner_images);
  694. $banner_desc = $box->box_banner_images_desc;
  695. $banner_desc = $banner_desc ? json_decode($banner_desc, true) : [];
  696. foreach ($box->box_banner_images as $index => $image) {
  697. $image = $image ? cdnurl($image, true) : '';
  698. $box_banner_images[] = $image;
  699. $box_banner[] = [
  700. 'desc' => $banner_desc[$index] ?? '',
  701. 'image' => $image
  702. ];
  703. }
  704. $ret = [
  705. 'mycoin' => $mycoin,
  706. 'is_star' => $is_star,
  707. 'box_banner_images' => $box_banner_images,
  708. 'box_banner' => $box_banner,
  709. 'box_name' => $box->box_name,
  710. 'coin_price' => intval($box->coin_price),
  711. 'goodslist' => $firstGoods,
  712. 'more' => [
  713. 'goodslist' => $moreGoods,
  714. 'tags' => $tags
  715. ],
  716. 'record' => $prize
  717. ];
  718. $this->success('查询成功', $ret);
  719. }
  720. /**
  721. * 查询更多盲盒开箱记录
  722. * @throws DbException
  723. * @author fuyelk <fuyelk@fuyelk.com>
  724. */
  725. public function boxOpenRecord()
  726. {
  727. $box_id = input('box_id/d');
  728. $pagesize = input('pagesize/d', 10);
  729. $page = input('page/d', 1);
  730. if (empty($box_id)) {
  731. $this->error('请选择盲盒');
  732. }
  733. // 查询该盲盒开箱记录
  734. $record = Prizerecord::alias('prize')
  735. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  736. ->field('user.nickname,user.avatar')
  737. ->join('user user', 'user.id = prize.user_id')
  738. ->where('prize.box_id', $box_id)
  739. ->order('prize.id', 'desc')
  740. ->paginate($pagesize, false, ['page' => $page])
  741. ->each(function ($item) {
  742. $item->create_time = date('Y-m-d H:i:s', $item->create_time);
  743. $item->avatar = $item->avatar ? cdnurl($item->avatar, true) : letter_avatar($item->nickname);
  744. $item->goods_image = $item->goods_image ? cdnurl($item->goods_image, true) : '';
  745. });
  746. $this->success('查询成功', $record);
  747. }
  748. /**
  749. * 试一试盲盒详情
  750. * @throws \think\db\exception\DataNotFoundException
  751. * @throws \think\db\exception\ModelNotFoundException
  752. * @throws \think\exception\DbException
  753. * @author fuyelk <fuyelk@fuyelk.com>
  754. */
  755. public function tryBoxDetail()
  756. {
  757. // 查询试一试盲盒
  758. $box_id = Box::where('is_try', 1)->value('id');
  759. // 查询金币余额
  760. $mycoin = $this->auth->isLogin() ? $this->auth->coin : 0;
  761. // 查询是否收藏
  762. $is_star = $this->auth->isLogin() ? Star::check($this->auth->id, $box_id) : 0;
  763. // 查询盲盒基础信息
  764. $box = Box::field('box_banner_images,box_name,coin_price')->where('id', $box_id)->find();
  765. if (empty($box)) {
  766. $this->error('盲盒有误');
  767. }
  768. $tagName = [
  769. 'normal' => '普通',
  770. 'rare' => '稀有',
  771. 'supreme' => '史诗',
  772. 'legend' => '传说',
  773. ];
  774. // 查询商品id及概率
  775. $detail = Detail::where('box_id', $box_id)->order('weigh', 'desc')->column('rate', 'goods_id');
  776. // 查询前6个商品
  777. $firstGoods = Goods::field('image,coin_price,goods_name,tag')
  778. ->where('status', 'online')
  779. ->whereIn('id', array_slice(array_keys($detail), 0, 6))
  780. ->select();
  781. foreach ($firstGoods as &$first) {
  782. $first->image = $first->image ? cdnurl($first->image, true) : $first->image;
  783. $first->tag = $tagName[$first->tag];
  784. $first->price = round(Setting::getRmbFromCoin($first->coin_price ?: 0), 2);
  785. $first->hidden(['coin_price']);
  786. }
  787. // 查询全部商品
  788. $moreGoods = Goods::field('id,image,coin_price,goods_name,tag')
  789. ->where('status', 'online')
  790. ->whereIn('id', array_keys($detail))
  791. ->select();
  792. // 整理商品信息并记录每个类别的概率总合
  793. $rateList = [];
  794. foreach ($moreGoods as &$more) {
  795. if (isset($rateList[$more->tag])) {
  796. $rateList[$more->tag] += $detail[$more->id];
  797. } else {
  798. $rateList[$more->tag] = $detail[$more->id];
  799. }
  800. $more->image = $more->image ? cdnurl($more->image, true) : $more->image;
  801. $more->tag = $tagName[$more->tag];
  802. $more->price = round(Setting::getRmbFromCoin($more->coin_price ?: 0), 2);
  803. $more->hidden(['id,coin_price']);
  804. }
  805. $tags = [
  806. 'normal' => 0,
  807. 'rare' => 0,
  808. 'supreme' => 0,
  809. 'legend' => 0
  810. ];
  811. // 没有的商品概率设为0
  812. foreach ($tags as $tag => &$rate) {
  813. if (isset($rateList[$tag])) {
  814. $rate = $rateList[$tag];
  815. }
  816. }
  817. // 计算全部类别概率总和
  818. $rate_sum = array_sum(array_values($tags));
  819. // 计算每个类别概率
  820. foreach ($tags as $tag => &$rate) {
  821. $rate = round($rate / $rate_sum, 4) * 100 . '%';
  822. }
  823. // 查询该盲盒开箱记录
  824. $prize = Prizerecord::alias('prize')
  825. ->field('prize.goods_name,prize.goods_image,prize.goods_rmb_price,prize.create_time')
  826. ->field('user.nickname,user.avatar')
  827. ->join('user user', 'user.id = prize.user_id')
  828. ->where('prize.box_id', $box_id)
  829. ->order('prize.id', 'desc')
  830. ->limit(3)
  831. ->select();
  832. foreach ($prize as $prize_item) {
  833. $prize_item->create_time = date('Y-m-d H:i:s', $prize_item->create_time);
  834. $prize_item->avatar = $prize_item->avatar ? cdnurl($prize_item->avatar, true) : letter_avatar($prize_item->nickname);
  835. $prize_item->goods_image = $prize_item->goods_image ? cdnurl($prize_item->goods_image, true) : '';
  836. }
  837. $box_banner_images = [];
  838. $box->box_banner_images = explode(',', $box->box_banner_images);
  839. foreach ($box->box_banner_images as $box_banner_image) {
  840. $box_banner_images[] = cdnurl($box_banner_image, true);
  841. }
  842. $ret = [
  843. 'box_id' => $box_id,
  844. 'mycoin' => $mycoin,
  845. 'is_star' => $is_star,
  846. 'box_banner_images' => $box_banner_images,
  847. 'box_name' => $box->box_name,
  848. 'coin_price' => intval($box->coin_price),
  849. 'goodslist' => $firstGoods,
  850. 'more' => [
  851. 'goodslist' => $moreGoods,
  852. 'tags' => $tags
  853. ],
  854. 'record' => $prize
  855. ];
  856. $this->success('查询成功', $ret);
  857. }
  858. /**
  859. * 抽奖测试
  860. * @author fuyelk <fuyelk@fuyelk.com>
  861. */
  862. public function test()
  863. {
  864. $prize = [];
  865. $times = $index = 1000;
  866. while (--$index >= 0) {
  867. $p = Detail::getOne(1);
  868. if (isset($prize[$p])) {
  869. $prize[$p]++;
  870. } else {
  871. $prize[$p] = 1;
  872. }
  873. }
  874. foreach ($prize as $goods => &$rate) {
  875. $rate = round($rate / $times, 2) * 100;
  876. }
  877. arsort($prize);
  878. de($prize);
  879. }
  880. /**
  881. * 点赞
  882. * @author fuyelk <fuyelk@fuyelk.com>
  883. */
  884. public function star()
  885. {
  886. $box_id = input('box_id/d');
  887. try {
  888. $res = Star::click($this->auth->id, $box_id);
  889. } catch (Exception $e) {
  890. $this->error('收藏失败');
  891. }
  892. if ($res) {
  893. $this->success('收藏成功', ['is_star' => 1]);
  894. }
  895. $this->success('取消收藏成功', ['is_star' => 0]);
  896. }
  897. /**
  898. * 试玩
  899. * @throws DataNotFoundException
  900. * @throws DbException
  901. * @throws ModelNotFoundException
  902. * @author fuyelk <fuyelk@fuyelk.com>
  903. */
  904. public function haveATry()
  905. {
  906. $box_id = input('box_id/d');
  907. $num = input('num/d');
  908. $select = input('select', '随机');
  909. if (empty($box_id)) {
  910. $this->error('请选择盲盒');
  911. }
  912. if (!in_array($num, [1, 5])) {
  913. $this->error('开箱数量有误');
  914. }
  915. // 检查盲盒是否可以试玩
  916. // $box = Box::where('id', $box_id)->where('is_try', 1)->value('id');
  917. // if (empty($box)) {
  918. // $this->error('该盲盒暂时不可试玩');
  919. // }
  920. try {
  921. if (1 == $num) {
  922. $goodsIds = [Detail::getOnes($box_id)];
  923. } else {
  924. $goodsIds = Detail::getMores($box_id, $num);
  925. }
  926. } catch (\Exception $e) {
  927. $this->error($e->getMessage());
  928. }
  929. foreach ($goodsIds as $goodsId) {
  930. $goodsInfo[] = Goods::field('image,goods_name,tag')->where('id', $goodsId)->find();
  931. }
  932. if (empty($goodsInfo)) {
  933. $this->error('抽奖失败');
  934. }
  935. foreach ($goodsInfo as $item) {
  936. if ($item->image) {
  937. $item->image = cdnurl($item->image, true);
  938. }
  939. }
  940. $ret = [
  941. 'select' => $select,
  942. 'goodsInfo' => $goodsInfo
  943. ];
  944. $this->success('抽奖成功', $ret);
  945. }
  946. /**
  947. * 创建盲盒订单
  948. * @author fuyelk <fuyelk@fuyelk.com>
  949. */
  950. public function createOrder()
  951. {
  952. $box_id = input('box_id/d');
  953. $num = input('num/d');
  954. $select = input('select', '');
  955. if (empty($box_id)) {
  956. $this->error('未选择盲盒');
  957. }
  958. if (!in_array($num, [1, 5, 9])) {
  959. $this->error('选择的盲盒数量有误');
  960. }
  961. // 检查盲盒
  962. $box = Box::field('id,box_name,box_banner_images,coin_price,box_banner_images')->where('id', $box_id)->find();
  963. if (empty($box)) {
  964. $this->error('选择的盲盒有误');
  965. }
  966. // 检查盲盒奖品
  967. $prize = Detail::where('box_id', $box_id)->order('weigh desc,id asc')->value('goods_id');
  968. if (empty($prize)) {
  969. $this->error('这个盲盒太火爆吧,商品暂时缺货!');
  970. }
  971. // 查询前6个商品的图片
  972. $goodsIds = Detail::where('box_id', $box_id)->order('weigh desc,id asc')->column('goods_id');
  973. $goodsImages = Goods::where('status', 'online')
  974. ->whereIn('id', $goodsIds)
  975. ->column('image');
  976. foreach ($goodsImages as &$image) {
  977. $image = cdnurl($image, true);
  978. }
  979. Db::startTrans();
  980. try {
  981. $rmb_price = round(Setting::getRmbFromCoin($box->coin_price ?: 0), 2);
  982. $res = Order::create([
  983. 'box_id' => $box_id,
  984. 'box_name' => $box->box_name,
  985. 'image' => $goodsImages[0] ?? '',
  986. 'coin_price' => $box->coin_price,
  987. 'rmb_price' => $rmb_price,
  988. 'num' => $num,
  989. 'coin_amount' => $box->coin_price * $num,
  990. 'rmb_amount' => $rmb_price * $num,
  991. 'user_id' => $this->auth->id,
  992. 'select' => $select,
  993. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  994. ]);
  995. $ret = [
  996. 'order_id' => intval($res->id),
  997. 'box_name' => $res->box_name,
  998. 'images' => $goodsImages,
  999. 'coin_amount' => $res->coin_amount,
  1000. 'rmb_amount' => $res->rmb_amount,
  1001. 'notice' => Text::getText('pay_tipstips'),
  1002. 'coin_not_enough' => !!(intval($this->auth->coin) < intval($res->coin_amount)),
  1003. 'alipay' => $this->request->domain() . '/api/alipay/boxpay/orderid/' . intval($res->id),
  1004. 'wechat' => '/api/wechat/boxpay/orderid/' . intval($res->id),
  1005. ];
  1006. } catch (Exception $e) {
  1007. Db::rollback();
  1008. $this->error('创建订单失败');
  1009. }
  1010. Db::commit();
  1011. $this->success('创建订单成功', $ret);
  1012. }
  1013. /**
  1014. * 创建充值订单
  1015. * @author fuyelk <fuyelk@fuyelk.com>
  1016. */
  1017. public function createRechargeOrder()
  1018. {
  1019. $amount = input('amount/d');
  1020. if (empty($amount)) {
  1021. $this->error('金额不能为空');
  1022. }
  1023. // 检查金额是否允许
  1024. $rmb = RechargeList::where('coin', intval($amount))->value('rmb');
  1025. if (empty($rmb)) {
  1026. $this->error('充值金额有误');
  1027. }
  1028. Db::startTrans();
  1029. try {
  1030. $res = RechargeOrder::create([
  1031. 'user_id' => $this->auth->id,
  1032. 'coin_amount' => intval($amount),
  1033. 'rmb_amount' => round($rmb, 2),
  1034. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  1035. ]);
  1036. } catch (\Exception $e) {
  1037. Db::rollback();
  1038. $this->error('创建订单出错');
  1039. }
  1040. Db::commit();
  1041. $ret = [
  1042. 'alipay' => $this->request->domain() . '/api/alipay/rechargepay/orderid/' . intval($res->id),
  1043. 'wechat' => '/api/wechat/rechargepay/orderid/' . intval($res->id),
  1044. 'eepay' => '/api/eepay/rechargepay/orderid/' . intval($res->id),
  1045. ];
  1046. $this->success('创建订单成功', $ret);
  1047. }
  1048. /**
  1049. * 创建充值订单
  1050. * @author fuyelk <fuyelk@fuyelk.com>
  1051. */
  1052. public function createRechargeOrder_bak()
  1053. {
  1054. $amount = input('amount/d');
  1055. if (empty($amount)) {
  1056. $this->error('金额不能为空');
  1057. }
  1058. // 检查金额是否允许
  1059. $rmb = RechargeList::where('coin', intval($amount))->value('rmb');
  1060. if (empty($rmb)) {
  1061. $this->error('充值金额有误');
  1062. }
  1063. Db::startTrans();
  1064. try {
  1065. $res = RechargeOrder::create([
  1066. 'user_id' => $this->auth->id,
  1067. 'coin_amount' => intval($amount),
  1068. 'rmb_amount' => round($rmb, 2),
  1069. 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999)
  1070. ]);
  1071. } catch (\Exception $e) {
  1072. Db::rollback();
  1073. $this->error('创建订单出错');
  1074. }
  1075. Db::commit();
  1076. $ret = [
  1077. 'alipay' => $this->request->domain() . '/api/alipay/rechargepay/orderid/' . intval($res->id),
  1078. 'wechat' => '/api/wechat/rechargepay/orderid/' . intval($res->id),
  1079. ];
  1080. $this->success('创建订单成功', $ret);
  1081. }
  1082. /**
  1083. * 金币支付
  1084. * @author fuyelk <fuyelk@fuyelk.com>
  1085. */
  1086. public function coinPay()
  1087. {
  1088. $order_id = input('order_id/d');
  1089. if (empty($order_id)) {
  1090. $this->error('请选择支付订单');
  1091. }
  1092. $order = Order::field('id,box_id,num,status,coin_amount,out_trade_no,select')
  1093. ->where('id', $order_id)
  1094. ->where('user_id', $this->auth->id)
  1095. ->find();
  1096. if (empty($order)) {
  1097. $this->error('订单不存在');
  1098. }
  1099. if ('unpay' != $order->status) {
  1100. $this->error('该订单已支付,请勿重复支付');
  1101. }
  1102. // 查询用户余额
  1103. if (intval($this->auth->coin) < $order->coin_amount) {
  1104. $this->error('您的金币不足');
  1105. }
  1106. Db::startTrans();
  1107. try {
  1108. // 更新订单信息
  1109. $order->pay_method = 'coin';
  1110. $order->pay_coin = $order->coin_amount;
  1111. $order->pay_time = time();
  1112. $order->status = 'unused';// 状态:unpay=待支付,unused=待抽奖,used=已使用
  1113. $order->backend_read = 0;
  1114. $order->save();
  1115. $coin_before = $this->auth->coin;
  1116. // 减少金币余额
  1117. $user = $this->auth->getUser();
  1118. $user->setDec('coin', $order->pay_coin);
  1119. // 创建金币使用记录
  1120. CoinRecord::create([
  1121. 'user_id' => $this->auth->id,
  1122. 'before' => $coin_before,
  1123. 'after' => $this->auth->coin,
  1124. 'coin' => -$order->pay_coin,
  1125. 'order_id' => $order->id,
  1126. 'type' => 'pay_box', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入
  1127. ]);
  1128. $users = Db::table('box_user')->where('id', $this->auth->pid)->find();
  1129. $orderadd = Order::where('id', $order_id)->where('user_id', $this->auth->id)->find();
  1130. $lou = Db::table('box_setting')->where('id', 1)->find();
  1131. $kou = $lou['kou'];
  1132. if ($users['recharnum'] == 1) {
  1133. Db::table('box_user')->where('id', $users['id'])->setInc("recharnum", 1);
  1134. Retail::giveMoneys($orderadd);
  1135. } else {
  1136. Db::table('box_user')->where('id', $users['id'])->setInc("recharnum", 1);
  1137. $userarr = Db::table('box_user')->where('id', $users['id'])->find();
  1138. if (bcmod($userarr['recharnum'], $kou) == 0) {
  1139. } else {
  1140. Retail::giveMoneys($orderadd);
  1141. };
  1142. }
  1143. // print_r(bcmod($users['recharnum'],$kou);
  1144. } catch (\Exception $e) {
  1145. Db::rollback();
  1146. dta('用户支付失败,订单已回滚到待支付');
  1147. $this->error('支付失败');
  1148. }
  1149. Db::commit();
  1150. // 开箱
  1151. $prize = $this->open($order);
  1152. $this->success('支付成功', ['prize' => $prize]);
  1153. }
  1154. /**
  1155. * 余额支付
  1156. * @author fuyelk <fuyelk@fuyelk.com>
  1157. */
  1158. public function cmoneyPay()
  1159. {
  1160. $order_id = input('order_id/d');
  1161. if (empty($order_id)) {
  1162. $this->error('请选择支付订单');
  1163. }
  1164. $order = Order::field('id,box_id,num,status,rmb_amount,out_trade_no,select')
  1165. ->where('id', $order_id)
  1166. ->where('user_id', $this->auth->id)
  1167. ->find();
  1168. if (empty($order)) {
  1169. $this->error('订单不存在');
  1170. }
  1171. if ('unpay' != $order->status) {
  1172. $this->error('该订单已支付,请勿重复支付');
  1173. }
  1174. // 查询用户余额
  1175. if (intval($this->auth->money) < $order->rmb_amount) {
  1176. $this->error('您的余额不足');
  1177. }
  1178. Db::startTrans();
  1179. try {
  1180. // 更新订单信息
  1181. $order->pay_method = 'money';
  1182. $order->rmb_amount = $order->rmb_amount;
  1183. $order->pay_time = time();
  1184. $order->status = 'unused';// 状态:unpay=待支付,unused=待抽奖,used=已使用
  1185. $order->backend_read = 0;
  1186. $order->save();
  1187. $coin_before = $this->auth->money;
  1188. // 减少余额
  1189. $user = $this->auth->getUser();
  1190. $user->setDec('money', $order->rmb_amount);
  1191. // 创建余额使用记录
  1192. MoneyRecord::create([
  1193. 'user_id' => $this->auth->id,
  1194. 'before' => $this->auth->money,
  1195. 'after' => $this->auth->money - $order->rmb_amount,
  1196. 'money' => -$order->rmb_amount,
  1197. 'order_id' => $order->id,
  1198. 'type' => 'yezhifu', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败,yezhifu=余额支付
  1199. ]);
  1200. // $users= Db::table('box_user') ->where('id',$this->auth->pid)->find();
  1201. // $orderadd = Order::where('id', $order_id)->where('user_id', $this->auth->id)->find();
  1202. // $lou = Db::table('box_setting')->where('id', 1)->find();
  1203. // $kou = $lou['kou'];
  1204. // if($users['recharnum'] == 1){
  1205. // Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1206. // Retail::giveMoneys($orderadd);
  1207. // }else{
  1208. // Db::table('box_user') ->where('id',$users['id'])->setInc("recharnum", 1);
  1209. // $userarr= Db::table('box_user') ->where('id',$users['id'])->find();
  1210. // // if(bcmod($userarr['recharnum'],$kou) == 0){
  1211. // // }else{
  1212. // // Retail::giveMoneys($orderadd);
  1213. // // };
  1214. // }
  1215. // print_r(bcmod($users['recharnum'],$kou);
  1216. } catch (\Exception $e) {
  1217. Db::rollback();
  1218. dta('用户支付失败,订单已回滚到待支付');
  1219. $this->error('支付失败');
  1220. }
  1221. Db::commit();
  1222. // 开箱
  1223. $prize = $this->open($order);
  1224. $this->success('支付成功', ['prize' => $prize]);
  1225. }
  1226. /**
  1227. * 价格区间
  1228. * @author fuyelk <fuyelk@fuyelk.com>
  1229. */
  1230. public function priceRange()
  1231. {
  1232. $list = PriceRange::order('weigh desc,id asc')->column('range');
  1233. $this->success('查询成功', ['range' => $list]);
  1234. }
  1235. /**
  1236. * 充值列表
  1237. * @author fuyelk <fuyelk@fuyelk.com>
  1238. */
  1239. public function rechargeList()
  1240. {
  1241. $coinList = RechargeList::order('weigh desc,id desc')->column('coin,rmb', 'id');
  1242. $list = [];
  1243. foreach ($coinList as $item) {
  1244. $list[] = [
  1245. 'coin' => $item['coin'],
  1246. 'rmb' => floatval($item['rmb'])
  1247. ];
  1248. }
  1249. $num = Setting::getSetting('one_rmb_to_coin_num');
  1250. $ret = [
  1251. 'tips' => '充值金额数值比例1:' . round(Setting::getSetting('one_rmb_to_coin_num'), 2),
  1252. 'list' => $list,
  1253. 'bili' => $num,
  1254. 'notice' => Text::getText('recharge_introduction')
  1255. ];
  1256. $this->success('查询成功', $ret);
  1257. }
  1258. /**
  1259. * 开箱
  1260. * @param Order $order 订单
  1261. * @return array|bool
  1262. * @author fuyelk <fuyelk@fuyelk.com>
  1263. */
  1264. private function open(Order $order)
  1265. {
  1266. // 检查订单状态
  1267. if ('unused' != $order->status) { // 状态:unpay=待支付,unused=待抽奖,used=已使用
  1268. return false;
  1269. }
  1270. if (empty($order->box_id)) {
  1271. $this->error('请选择盲盒');
  1272. }
  1273. if (!in_array($order->num, [1, 5, 9])) {
  1274. $this->error('开箱数量有误');
  1275. }
  1276. // 检查盲盒是否可以试玩
  1277. $box = Box::where('id', $order->box_id)->value('id');
  1278. if (empty($box)) {
  1279. $this->error('盲盒有误');
  1280. }
  1281. try {
  1282. // 抽奖 begin
  1283. if (1 == $order->num) {
  1284. $goodsIds = [Detail::getOne($order->box_id)];
  1285. } else {
  1286. $goodsIds = Detail::getMore($order->box_id, $order->num);
  1287. }
  1288. } catch (\Exception $e) {
  1289. // 退款
  1290. if (!$this->refund($order)) {
  1291. $logID = "";
  1292. try {
  1293. $logID = dta($order->toArray(), '用户退款失败');
  1294. } catch (Exception $e) {
  1295. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1296. }
  1297. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1298. }
  1299. $this->error('抽奖失败,已退款');
  1300. }
  1301. // 抽奖 end
  1302. // 查询抽中的奖品信息
  1303. foreach ($goodsIds as $goodsId) {
  1304. $goodsInfo[] = Goods::field('id,image,tag,coin_price,goods_name,delivery_fee')->where('id', $goodsId)->find();
  1305. }
  1306. // 抽奖失败
  1307. if (empty($goodsInfo)) {
  1308. // 退款
  1309. if (!$this->refund($order)) {
  1310. $logID = "";
  1311. try {
  1312. $logID = dta($order->toArray(), '用户退款失败');
  1313. } catch (Exception $e) {
  1314. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1315. }
  1316. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1317. }
  1318. $this->error('抽奖失败,已退款');
  1319. }
  1320. $prizeInfo = [];
  1321. Db::startTrans();
  1322. try {
  1323. foreach ($goodsInfo as &$goods) {
  1324. // 创建开箱记录
  1325. $prize = Prizerecord::create([
  1326. 'box_id' => $order->box_id,
  1327. 'order_id' => $order->id,
  1328. 'out_trade_no' => $order->out_trade_no,
  1329. 'user_id' => $this->auth->id,
  1330. 'goods_id' => $goods->id,
  1331. 'goods_name' => $goods->goods_name,
  1332. 'goods_image' => $goods->image,
  1333. 'goods_coin_price' => $goods->coin_price,
  1334. 'goods_rmb_price' => round(Setting::getRmbFromCoin($goods->coin_price ?: 0), 2),
  1335. 'delivery_fee' => round($goods->delivery_fee, 2),
  1336. 'status' => 'bag', // 奖品状态:bag=盒柜,exchange=已回收,delivery=申请发货,received=已收货
  1337. ]);
  1338. // 减少商品库存
  1339. Goods::where('id', $goods->id)->setDec('stock');
  1340. $prizeInfo[] = [
  1341. 'prize_id' => intval($prize->id),
  1342. 'image' => $prize->goods_image ? cdnurl($prize->goods_image, true) : '',
  1343. 'goods_name' => $prize->goods_name,
  1344. 'tag' => $goods->tag
  1345. ];
  1346. }
  1347. // 增加盲盒销量
  1348. Box::where('id', $order->box_id)->setInc('sales', $order->num);
  1349. } catch (\Exception $e) {
  1350. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '保存开箱记录、减少商品库存失败');
  1351. Db::rollback();
  1352. // 退款
  1353. if (!$this->refund($order)) {
  1354. $logID = "";
  1355. try {
  1356. $logID = dta($order->toArray(), '用户退款到金币失败');
  1357. } catch (Exception $e) {
  1358. $logID = dta(['order_id' => $order->id], '用户退款失败');
  1359. }
  1360. $this->error('库存不足退款失败,请截屏联系平台:' . $logID);
  1361. }
  1362. $this->error('抽奖失败,已退款');
  1363. }
  1364. Db::commit();
  1365. // 订单状态改为已使用
  1366. $order->save(['status' => 'used', 'backend_read' => 0]);
  1367. $ret = [
  1368. 'select' => $order->select,
  1369. 'prizeInfo' => $prizeInfo
  1370. ];
  1371. return $ret;
  1372. }
  1373. /**
  1374. * 通过订单号打开盲盒
  1375. * @author fuyelk <fuyelk@fuyelk.com>
  1376. */
  1377. public function openByOrderTrade()
  1378. {
  1379. $out_trade_no = input('out_trade_no');
  1380. $order_id = input('order_id');
  1381. if (empty($out_trade_no) && empty($order_id)) {
  1382. $this->error('参数有误');
  1383. }
  1384. $where = [];
  1385. if (!empty($out_trade_no)) {
  1386. $where['out_trade_no'] = $out_trade_no;
  1387. }
  1388. if (!empty($order_id)) {
  1389. $where['id'] = $order_id;
  1390. }
  1391. $order = Order::where($where)->where('user_id', $this->auth->id)->where('status', 'unused')->find();
  1392. if (empty($order)) {
  1393. $this->error('订单有误');
  1394. }
  1395. // 开箱
  1396. $prize = $this->open($order);
  1397. $this->success('开箱成功', ['prize' => $prize]);
  1398. }
  1399. /**
  1400. * 退款自动选择
  1401. * @param Order $order
  1402. * @return bool
  1403. * @author fuyelk <fuyelk@fuyelk.com>
  1404. */
  1405. private function refund(Order $order)
  1406. {
  1407. switch ($order->pay_method) {
  1408. case 'coin' :
  1409. return $this->refundToCoin($order);
  1410. break;
  1411. case 'wechat':
  1412. return $this->refundToMoney($order);
  1413. break;
  1414. case 'alipay':
  1415. return $this->refundToMoney($order);
  1416. break;
  1417. default:
  1418. return false;
  1419. }
  1420. }
  1421. /**
  1422. * 退款到金币
  1423. * @param Order $order 订单
  1424. * @author fuyelk <fuyelk@fuyelk.com>
  1425. */
  1426. private function refundToCoin(Order $order)
  1427. {
  1428. $coin_before = $this->auth->coin;
  1429. Db::startTrans();
  1430. try {
  1431. // 增加金币余额
  1432. $user = $this->auth->getUser();
  1433. $user->setInc('coin', $order->pay_coin);
  1434. // 创建金币记录
  1435. CoinRecord::create([
  1436. 'user_id' => $this->auth->id,
  1437. 'before' => $coin_before,
  1438. 'after' => $this->auth->coin,
  1439. 'coin' => $order->pay_coin,
  1440. 'order_id' => $order->id,
  1441. 'type' => 'refund', // 变更类型:pay_box=支付盲盒,recharge=充值,fromwallet=余额转入,refund=退款
  1442. ]);
  1443. // 更新订单状态为已退款
  1444. $order->save(['status' => 'refund', 'backend_read' => 0]);
  1445. } catch (\Exception $e) {
  1446. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '用户退款到金币失败');
  1447. Db::rollback();
  1448. return false;
  1449. }
  1450. Db::commit();
  1451. return true;
  1452. }
  1453. /**
  1454. * 退款到余额
  1455. * @param Order $order 订单
  1456. * @author fuyelk <fuyelk@fuyelk.com>
  1457. */
  1458. private function refundToMoney(Order $order)
  1459. {
  1460. $money_before = $this->auth->money;
  1461. Db::startTrans();
  1462. try {
  1463. // 增加金币余额
  1464. $user = $this->auth->getUser();
  1465. $user->setInc('money', $order->pay_rmb);
  1466. // 创建余额记录
  1467. MoneyRecord::create([
  1468. 'user_id' => $this->auth->id,
  1469. 'before' => $money_before,
  1470. 'after' => $this->auth->money,
  1471. 'money' => $order->pay_rmb,
  1472. 'order_id' => $order->id,
  1473. 'type' => 'refund', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  1474. ]);
  1475. // 更新订单状态为已退款
  1476. $order->save(['status' => 'refund', 'backend_read' => 0]);
  1477. } catch (\Exception $e) {
  1478. dta(['order_id' => $order->id, 'error' => $e->getMessage()], '用户退款到余额失败');
  1479. Db::rollback();
  1480. return false;
  1481. }
  1482. Db::commit();
  1483. return true;
  1484. }
  1485. /**
  1486. * 获取客服信息
  1487. * @author fuyelk <fuyelk@fuyelk.com>
  1488. */
  1489. public function getServiceInfo()
  1490. {
  1491. $ret = [
  1492. 'number' => Setting::getSetting('service_number') ?: '',
  1493. 'qrcode' => ($qrcode = Setting::getSetting('service_qrcode')) ? cdnurl($qrcode, true) : '',
  1494. ];
  1495. $this->success('查询成功', $ret);
  1496. }
  1497. /**
  1498. * 获取落地域名
  1499. * @author fuyelk <fuyelk@fuyelk.com>
  1500. */
  1501. public function getServiceurl()
  1502. {
  1503. $ret = Setting::where('id', 1)->find();
  1504. // $curl = Setting::getSetting('curl');
  1505. // $abs = explode('|',$curl);
  1506. // $ret = [
  1507. // 'url' => Setting::getSetting('url')
  1508. // ];
  1509. $this->success('查询成功', $ret['url']);
  1510. }
  1511. /**
  1512. * 获取微信登录地址
  1513. * @author fuyelk <fuyelk@fuyelk.com>
  1514. */
  1515. public function getWechatLoginUrl()
  1516. {
  1517. $redirect = input('redirect');
  1518. if (empty($redirect)) {
  1519. $this->error('重定向地址不能为空');
  1520. }
  1521. $redirect = preg_match("/^http(.*)/", $redirect) ? $redirect : $this->request->domain() . $redirect;
  1522. $ret = [
  1523. 'url' => $this->request->domain() . '/index/wechat/bootToUrl?url=' . base64_encode($redirect)
  1524. ];
  1525. $this->success('查询成功', $ret);
  1526. }
  1527. /**
  1528. * 添加晒图
  1529. */
  1530. public function setShai()
  1531. {
  1532. $validate = new \think\Validate([
  1533. 'box_zw' => 'require',
  1534. 'box_img' => 'require',
  1535. 'token' => 'require',
  1536. ]);
  1537. $validate->message([
  1538. 'box_zw.require' => '盲盒详情banner文字不能为空',
  1539. 'box_img.require' => '盲盒详情banner不能为空',
  1540. 'token.require' => 'token不能为空',
  1541. ]);
  1542. $data = $this->request->param();
  1543. if (!$validate->check($data)) {
  1544. $this->error($validate->getError());
  1545. }
  1546. $params = [
  1547. 'box_mc' => $this->auth->nickname,
  1548. 'box_tx' => $this->auth->avatar ? cdnurl($this->auth->avatar, true) : '',
  1549. 'box_zw' => $data['box_zw'],
  1550. 'box_img' => $data['box_img'],
  1551. 'create_time' => time()
  1552. ];
  1553. Db::table('box_shai')->insertGetId($params);
  1554. $this->success();
  1555. }
  1556. }