OrderController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * 订单管理
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-03 16:51:03
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AlbumManufacturerModel;
  12. use App\Models\AlbumOrderModel;
  13. use App\Models\AlbumPartsModel;
  14. use App\Models\AlbumProgressModel;
  15. use App\Models\AlbumUserModel;
  16. use App\Models\FurnitureFormidModel;
  17. use App\Services\Base\Attachment;
  18. use EasyWeChat\Factory;
  19. use EasyWeChat\Payment\Order;
  20. use Image;
  21. use Illuminate\Http\Request;
  22. use App\Repositories\Base\Criteria\OrderBy;
  23. use App\Repositories\Album\Criteria\MultiWhere;
  24. use App\Repositories\Album\OrderRepository;
  25. use Illuminate\Support\Facades\DB;
  26. class OrderController extends Controller
  27. {
  28. private $repository;
  29. public function __construct(OrderRepository $repository) {
  30. if(!$this->repository) $this->repository = $repository;
  31. parent::__construct();
  32. }
  33. function index(Request $request) {
  34. $search['keyword'] = $request->input('keyword');
  35. $search['status'] = $request->input('status');
  36. $search['expected'] = $request->input('expected');
  37. $search['storeid'] = $this->getStoreId();
  38. $order = array();
  39. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  40. $order[$request['sort_field']] = $request['sort_field_by'];
  41. }else{
  42. $order['id']='DESC';
  43. }
  44. $list = $this->repository->searchOrder($search,$order);
  45. return view('admin.album.order.index',compact('list'));
  46. }
  47. function check(Request $request) {
  48. $data = $this->repository->find(request('id'));
  49. $img = explode(',',$data['picture']);
  50. $parts = AlbumPartsModel::where('store_id', $this->getStoreId())->where('order_id', request('id'))
  51. ->get(['name', 'count']);
  52. return view('admin.album.order.check',compact('data','img','parts'));
  53. }
  54. /**
  55. * 添加
  56. *
  57. */
  58. public function create(Request $request)
  59. {
  60. if($request->method() == 'POST') {
  61. return $this->_createSave();
  62. }
  63. return view('admin.album.order.edit');
  64. }
  65. /**
  66. * 保存修改
  67. */
  68. private function _createSave(){
  69. $data = (array) request('data');
  70. $data['store_id'] = $this->getStoreId();
  71. $array_pic = request('picture');
  72. if ($array_pic && isset($array_pic['url'])) {
  73. $data['picture'] = '';
  74. foreach ($array_pic['url'] as $item) {
  75. $data['picture'] .= $item . ',';
  76. }
  77. if ($data['picture']) $data['picture'] = rtrim($data['picture'], ',');
  78. } else {
  79. $data['picture'] = '';
  80. }
  81. $data['status'] = 0;
  82. $data['sno'] = date('YmdHmis');
  83. $res = $this->repository->create($data);
  84. if($res) {
  85. $this->getQrcode($this->getStoreId(),$res->id);
  86. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  87. $url[] = array('url'=>U( 'Album/Order/create'),'title'=>'继续添加');
  88. $this->showMessage('添加成功',$url);
  89. }else{
  90. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  91. return $this->showWarning('添加失败',$url);
  92. }
  93. }
  94. /**
  95. *
  96. * 修改
  97. *
  98. *
  99. */
  100. public function update(Request $request) {
  101. if($request->method() == 'POST') {
  102. return $this->_updateSave();
  103. }
  104. $data = $this->repository->find($request->get('id'));
  105. $data['picture'] = explode(',',$data['picture']);
  106. return view('admin.album.order.edit',compact('data'));
  107. }
  108. /**
  109. * 保存修改
  110. */
  111. private function _updateSave() {
  112. $data = (array) request('data');
  113. $array_pic = request('picture');
  114. if ($array_pic && isset($array_pic['url'])) {
  115. $data['picture'] = '';
  116. foreach ($array_pic['url'] as $item) {
  117. $data['picture'] .= $item . ',';
  118. }
  119. if ($data['picture']) $data['picture'] = rtrim($data['picture'], ',');
  120. } else {
  121. $data['picture'] = '';
  122. }
  123. $old = $this->repository->find(request('id'))->picture;
  124. $ok = $this->repository->update(request('id'),$data);
  125. if($ok) {
  126. //操作成功,删除原来的图片
  127. if ($old) {
  128. $pics = explode(',', $old);
  129. foreach ($pics as $pic) {
  130. if (!in_array($pic, $array_pic['url'])) {
  131. $md5 = $this->getarea($pic);
  132. $attache = new Attachment();
  133. $attache->deleteAttachment($md5);
  134. }
  135. }
  136. }
  137. return $this->showMessage('操作成功',urldecode(request('_referer')));
  138. }else{
  139. $url[] = array('url'=>U( 'Album/Order/index'),'title'=>'返回列表');
  140. return $this->showWarning('操作失败',$url);
  141. }
  142. }
  143. public function view(Request $request) {
  144. $data = $this->repository->find(request('id'));
  145. $img = explode(',',$data['picture']);
  146. $this->getQrcode($this->getStoreId(),request('id'));
  147. return view('admin.album.order.view',compact('data','img'));
  148. }
  149. public function picture(Request $request) {
  150. $data = $this->repository->find(request('id'));
  151. $img = explode(',',$data['picture']);
  152. return view('admin.album.order.picture',compact('img'));
  153. }
  154. public function addecomment(Request $request){
  155. $data = $this->repository->find(request('id'));
  156. $data->expected_comment = $request->get('expected_comment');
  157. $ok = $data->save();
  158. if($ok) {
  159. return $this->showMessage('操作成功');
  160. }else{
  161. return $this->showWarning('操作失败');
  162. }
  163. }
  164. /**
  165. *
  166. * 状态改变
  167. *
  168. */
  169. public function status(Request $request) {
  170. $data = [
  171. 'status'=>request('status'),
  172. 'price' =>request('price'),
  173. 'verifier_id' =>$this->_user->id,
  174. 'expected_time' =>request('expected_time'),
  175. ];
  176. $ok = $this->repository->update(request('id'),$data);
  177. if($ok) {
  178. foreach (request('name') as $k =>$v){
  179. $parts[$k]['name'] = $v;
  180. $parts[$k]['count'] = request('count')[$k];
  181. $parts[$k]['order_id'] = request('id');
  182. $parts[$k]['store_id'] = $this->getStoreId();
  183. if($parts[$k]['name']){
  184. AlbumPartsModel::create($parts[$k]);
  185. }
  186. }
  187. $data = [
  188. 'status' =>request('status'),
  189. 'order_id' =>request('id'),
  190. 'store_id' =>$this->getStoreId(),
  191. 'description' => '订单已审核,修理费为'.request('price').'订单下发到生产部,等待制作'
  192. ];
  193. AlbumProgressModel::create($data);
  194. $storeid =$this->getStoreId();
  195. $setting = AlbumManufacturerModel::where('store_id', $storeid)->first();
  196. $config = [
  197. 'app_id' => $setting->service_app_id,
  198. 'secret' => $setting->service_app_secret,
  199. // 下面为可选项
  200. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  201. 'response_type' => 'array',
  202. ];
  203. $app = Factory::miniProgram($config);
  204. $users = AlbumUserModel::where('store_id',1)->where('role',1)->get();
  205. // foreach ($users as $user){
  206. // $formid = FurnitureFormidModel::where('store_id',$storeid)->where(DB::raw("DATEDIFF(NOW(),created_at)"), '<', 7)->where('openid',$user->wechat_open_id)->where('status',0)->orderBy('created_at','asc')->first();
  207. // $app->template_message->send([
  208. // 'touser' => $user->wechat_open_id,
  209. // 'template_id' => '',
  210. // 'page' => 'index',
  211. // 'form_id' => $formid->formid,
  212. // 'data' => [
  213. // 'keyword1' => 'VALUE',
  214. // 'keyword2' => 'VALUE2',
  215. // // ...
  216. // ],
  217. // ]);
  218. // }
  219. return $this->showMessage('操作成功');
  220. }else{
  221. return $this->showWarning('操作失败');
  222. }
  223. }
  224. /**
  225. * 删除
  226. */
  227. public function destroy(Request $request) {
  228. $bool = $this->repository->destroy($request->get('id'));
  229. if($bool) {
  230. return $this->showMessage('操作成功');
  231. }else{
  232. return $this->showWarning("操作失败");
  233. }
  234. }
  235. public function getPic(Request $request){
  236. $order = AlbumOrderModel::find(request('id'));
  237. $img = $order->picture;
  238. $img = explode(',',$img);
  239. return json_encode($img);
  240. }
  241. public function getQrcode($store_id, $id)
  242. {
  243. $this->wechat_app = AlbumManufacturerModel::where('store_id', $store_id)->first();
  244. $config = [
  245. 'app_id' => $this->wechat_app->service_app_id,
  246. 'secret' => $this->wechat_app->service_app_secret,
  247. // 下面为可选项
  248. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  249. 'response_type' => 'array',
  250. ];
  251. $app = Factory::miniProgram($config);
  252. $response = $app->app_code->getQrCode('pages/search/search?order_id=' . $id,300);
  253. $path = public_path() . '/download';
  254. $qrcode = $response->save($path, $id);
  255. if ($qrcode) {
  256. $qrcode = env('APP_URL') . '/download/' . $id . '.jpg';
  257. $order = AlbumOrderModel::find($id);
  258. $order->qrcode = $qrcode;
  259. $order->save();
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }
  265. public function getarea($str)
  266. {
  267. $start = strripos($str, '/');
  268. $first = substr($str, $start + 1);
  269. $end = strripos($first, '.');
  270. $last = substr($first, 0, $end);
  271. return $last;
  272. }
  273. }