Package.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use think\Exception;
  6. use GuzzleHttp\Client;
  7. /**
  8. * 套餐管理
  9. */
  10. class Package extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. //列表
  15. public function lists(){
  16. $status = input('status',1);
  17. $keyword = input('keyword','');
  18. $where = [
  19. 'p.company_id' => $this->auth->company_id,
  20. 'p.status' => $status,
  21. ];
  22. if(!empty($keyword)){
  23. $where['p.title|p.info'] = ['LIKE','%'.$keyword.'%'];
  24. }
  25. $list = Db::name('package')->alias('p')
  26. ->field('p.*,type.title as servicetype_title')
  27. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  28. ->where($where)->order('p.id desc')->autopage()->select();
  29. //追加赠送
  30. if(!empty($list)){
  31. $package_ids = array_column($list,'id');
  32. $gift = Db::name('package_gift')->alias('gift')
  33. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  34. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  35. ->where('gift.package_id','IN',$package_ids)
  36. ->where('coupons.status',1)
  37. ->select();
  38. foreach($list as $key => &$val){
  39. $val['gift'] = [];
  40. foreach($gift as $k => $v){
  41. if($val['id'] == $v['package_id']){
  42. $val['gift'][] = $v;
  43. }
  44. }
  45. }
  46. }
  47. $list = list_domain_image($list,['images','content_images']);
  48. $this->success(1,$list);
  49. }
  50. //新增
  51. public function add(){
  52. Db::startTrans();
  53. try {
  54. //验证
  55. if($this->auth->type != 1){
  56. throw new Exception('只有门店老板才能设置');
  57. }
  58. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images','type'];
  59. $data = request_post_hub($field);
  60. $data['company_id'] = $this->auth->company_id;
  61. $data['createtime'] = time();
  62. $data['updatetime'] = time();
  63. $data['status'] = 1;
  64. $package_id = Db::name('package')->insertGetId($data);
  65. if (!$package_id) {
  66. throw new Exception('添加套餐失败');
  67. }
  68. if (isset($data['type']) && $data['type'] == 2) {
  69. $gift_data = input('gift_data','','trim');
  70. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  71. if(is_array($gift_data) && !empty($gift_data)){
  72. $package_gift = [];
  73. foreach($gift_data as $key => $val){
  74. $package_gift[] = [
  75. 'package_id' => $package_id,
  76. 'coupon_id' => $val['coupon_id'],
  77. 'number' => $val['number'],
  78. ];
  79. }
  80. if(!empty($package_gift)){
  81. $rs_gift = Db::name('package_gift')->insertAll($package_gift);
  82. if($rs_gift === false){
  83. Db::rollback();
  84. $this->error('添加失败');
  85. }
  86. }
  87. }
  88. }
  89. Db::commit();
  90. $this->success('添加成功');
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. }
  96. //上下架
  97. public function changestatus(){
  98. //验证
  99. if($this->auth->type != 1){
  100. $this->error('只有门店老板才能设置');
  101. }
  102. $id = input('id',0);
  103. $status = Db::name('package')->where('id',$id)->value('status');
  104. $new_status = $status == 1 ? 0 : 1;
  105. $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]);
  106. $this->success();
  107. }
  108. //详情
  109. public function info(){
  110. $id = input('id',0);
  111. $info = Db::name('package')->alias('p')
  112. ->field('p.*,type.title as servicetype_title')
  113. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  114. ->where('p.id',$id)->find();
  115. //追加赠送
  116. if(!empty($info)){
  117. $gift = Db::name('package_gift')->alias('gift')
  118. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  119. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  120. ->where('gift.package_id',$id)
  121. ->where('coupons.status',1)
  122. ->select();
  123. $info['gift'] = [];
  124. if (!empty($gift)) {
  125. foreach($gift as $k => $v){
  126. $info['gift'][] = $v;
  127. }
  128. }
  129. }
  130. $info = info_domain_image($info,['images','content_images']);
  131. $this->success(1,$info);
  132. }
  133. //编辑
  134. public function edit(){
  135. Db::startTrans();
  136. try {
  137. //验证
  138. if($this->auth->type != 1){
  139. throw new Exception('只有门店老板才能设置');
  140. }
  141. $id = input('id','');
  142. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  143. if(empty($check)){
  144. throw new Exception('不存在的套餐');
  145. }
  146. //
  147. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
  148. $data = request_post_hub($field);
  149. $data['updatetime'] = time();
  150. $packageRes = Db::name('package')->where('id',$id)->update($data);
  151. if (!$packageRes) {
  152. throw new Exception('套餐编辑失败');
  153. }
  154. if ($check['type'] == 2) {
  155. $packageGiftWhere['package_id'] = $id;
  156. $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select();
  157. $gift_data = input('gift_data','','trim');
  158. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  159. if(is_array($gift_data) && !empty($gift_data)) {
  160. $packageGiftCouponIds = array_column($packageGiftData,'coupon_id');
  161. $postCouponIds = array_column($gift_data,'coupon_id');
  162. $package_gift = [];
  163. $same = [];
  164. $different = [];
  165. foreach ($gift_data as $key => $val) {
  166. if (in_array($val['coupon_id'], $packageGiftCouponIds)) {
  167. $same[] = [
  168. 'package_id' => $id,
  169. 'coupon_id' => $val['coupon_id'],
  170. 'number' => $val['number'],
  171. ];
  172. } else {
  173. $different[] = [
  174. 'package_id' => $id,
  175. 'coupon_id' => $val['coupon_id'],
  176. 'number' => $val['number'],
  177. ];
  178. }
  179. }
  180. if (!empty($same)) {//相同的更新
  181. foreach ($same as $sameKey => $sameVal) {
  182. $sameWhere['package_id'] = $sameVal['package_id'];
  183. $sameWhere['coupon_id'] = $sameVal['coupon_id'];
  184. $sameData['number'] = $sameVal['number'];
  185. Db::name('package_gift')->where($sameWhere)->update($sameData);
  186. }
  187. }
  188. if (!empty($different)) {//新增的添加
  189. $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different);
  190. if (!$packageGiftRes) {
  191. throw new Exception('套餐卡券添加失败');
  192. }
  193. }
  194. //多余的删除
  195. $delIds = array_diff($packageGiftCouponIds,$postCouponIds);
  196. if (!empty($delIds)) {
  197. $packageGiftDelWhere['package_id'] = $id;
  198. $packageGiftDelWhere['coupon_id'] = ['in',$delIds];
  199. Db::name('package_gift')->where($packageGiftDelWhere)->delete();
  200. }
  201. } else {
  202. if (!empty($packageGiftData)) {
  203. $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete();
  204. if (!$packageGiftDelRes) {
  205. throw new Exception('删除卡券失败');
  206. }
  207. }
  208. }
  209. }
  210. Db::commit();
  211. $this->success('编辑成功');
  212. } catch (Exception $e) {
  213. Db::rollback();
  214. $this->error($e->getMessage());
  215. }
  216. }
  217. //删除
  218. public function delete(){
  219. //验证
  220. if($this->auth->type != 1){
  221. $this->error('只有门店老板才能设置');
  222. }
  223. $id = input('id','');
  224. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  225. if(empty($check)){
  226. $this->error('不存在的套餐');
  227. }
  228. Db::name('package')->where('id',$id)->delete();
  229. Db::name('package_gift')->where('package_id',$id)->delete();
  230. $this->success('删除成功');
  231. }
  232. //仅返回套餐二维码
  233. private function getMiniCode($id,$package,$companyid)
  234. {
  235. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  236. if (empty($package['mini_code'])) {
  237. $client = new Client();
  238. $tk = getAccessToken();
  239. $miniCodeConfig = config('param.mini_code');
  240. //$miniCodeConfig['env_version'] = 'trial'; //强制体验版
  241. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  242. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  243. 'json' => $miniCodeConfig,
  244. ]);
  245. $fileName = md5($id);
  246. $fileUrl = '/uploads/package/'.$fileName.'.png';
  247. $code = $res2->getBody()->getContents();
  248. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  249. $companyRes = Db::name('package')->where('id',$id)->update(['mini_code'=>$fileUrl]);
  250. if ($companyRes === false) {
  251. $this->error('生成套餐小程序码失败');
  252. }
  253. $miniCode = $httpStr.$fileUrl;
  254. } else {
  255. $miniCode = $httpStr.$package['mini_code'];
  256. }
  257. return $miniCode;
  258. }
  259. //生成视频分享海报
  260. public function showposter() {
  261. $id = input('id', 0, 'intval'); //id
  262. if (!$id) {
  263. $this->error('参数缺失');
  264. }
  265. $info = Db::name('package')->where('id',$id)->find();
  266. if (!$info) {
  267. $this->error('套餐不存在');
  268. }
  269. $inviteimage = $this->getMiniCode($id,$info,$this->auth->company_id);
  270. $package_image = explode(',',$info['images'])[0];
  271. $data = [
  272. [
  273. "left"=> "0px",
  274. "top"=> "0px",
  275. "type"=> "img",
  276. "width"=> "320px",
  277. "height"=> "290px",
  278. "src"=> one_domain_image($package_image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  279. ],
  280. [
  281. "left"=> "10px",
  282. "top"=> "370px",
  283. "type"=> "img",
  284. "width"=> "45px",
  285. "height"=> "45px",
  286. "src"=> one_domain_image($this->auth->company->image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  287. ],
  288. [
  289. "left"=> "10px",
  290. "top"=> "330px",
  291. "type"=> "nickname",
  292. "width"=> "166px",
  293. "height"=> "38px",
  294. "size"=> "11px",
  295. "color"=> "#333333",
  296. "content" => (iconv_strlen($info['title'], 'utf-8') <= 12 ? $info['title'] : mb_substr($info['title'], 0, 12) )
  297. ],
  298. [
  299. "left"=> "60px",
  300. "top"=> "385px",
  301. "type"=> "nickname",
  302. "width"=> "166px",
  303. "height"=> "38px",
  304. "size"=> "10px",
  305. "color"=> "#666666",
  306. "content" => (iconv_strlen($this->auth->company->name, 'utf-8') <= 8 ? $this->auth->company->name : mb_substr($this->auth->company->name, 0, 8))
  307. ],
  308. [
  309. "left"=> "195px",
  310. "top"=> "300px",
  311. "type"=> "img",
  312. "width"=> "110px",
  313. "height"=> "110px",
  314. "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  315. ],
  316. [
  317. "left"=> "200px",
  318. "top"=> "420px",
  319. "type"=> "nickname",
  320. "width"=> "166px",
  321. "height"=> "38px",
  322. "size"=> "9px",
  323. "color"=> "#666666",
  324. "content" => '长按扫码查看详情',
  325. ],
  326. ];
  327. $data = json_encode($data, 320);
  328. $poster = [
  329. 'id' => 1,
  330. 'title' => '测试',
  331. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  332. 'bg_image' => '/assets/img/posterbg.png',
  333. 'data' => $data,
  334. 'status' => 'normal',
  335. 'weigh' => 0,
  336. 'createtime' => 1653993709,
  337. 'updatetime' => 1653994259,
  338. ];
  339. $image = new \addons\poster\library\Image();
  340. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  341. if (!$imgurl) {
  342. $this->error('生成海报出错');
  343. }
  344. $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  345. //echo '<html><body><img src="'.$imgurl.'"></body></html>';exit;
  346. $this->success('', $imgurl);
  347. }
  348. }